'LINQ to XML display a combination of parent & child elements with grouping
I'm doing an internship trying to learn C#. I'm having trouble with my assignment on LINQ to XML. I have to display certain elements of an XML config file in Console. XML file:
<?xml version="1.0" encoding="utf-8" ?>
<config>
<login name = "user1">
<window title = "main">
<top>10</top>
<left>20</left>
<width>400</width>
<height>210</height>
</window>
<window title = "help">
<left>30</left>
<top>20</top>
<height>100</height>
</window>
</login>
<login name = "user2">
<window title = "main">
<top>20</top>
<left>20</left>
<width>400</width>
<height>220</height>
</window>
<window title = "help">
<left>30</left>
<top>20</top>
<height>100</height>
</window>
</login>
<login name = "user3">
<window title = "main">
<top>30</top>
<left>20</left>
<width>400</width>
<height>230</height>
</window>
<window title = "help">
<left>30</left>
<top>20</top>
<height>100</height>
</window>
</login>
</config>
Output to console should look like this:
Login: user1
main(10, 20, 400, 200)
help(20, 30, ?, 100)
Login: user2
...
So far I have this, but it's not right...
var query2 = from node in xml.Descendants("login")
from element in node.Elements()
select new { User = node.FirstAttribute, ElValue = element.Value, WinName = element.Attribute("title") };
foreach (var item in query2)
{
Console.WriteLine($" {item.User} \n {item.WinName} \n {item.ElValue}");
}
My output looks like this:
name="user1"
title="main"
1020400210
name="user1"
title="help"
3020100
name="user2"
title="main"
2020400220
name="user2"
title="help"
3020100
name="user3"
title="main"
3020400230
name="user3"
title="help"
3020100
Can anybody help me understand what I'm missing here and how to do it right? Thank you
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
