Reputation: 1012
I am iterating over an XML Node List which contains Gnomes (/GnomeArmy/Gnome), while I am iterating I would like to iterate through the list of children that belong to the gnome.
Currently I have it picking the first Gnome's children for both gnomes, this is incorrect as they both have there own children.
i.e. Gnome1 Children are Jessica & Nick, Gnome2 Children are also Jessica & Nick (this is wrong).
Thanks.
Code:
public static List<Gnome> ReadGnomes(string file)
{
List<Gnome> gnomeList = new List<Gnome>();
XmlDocument gnomeFile = new XmlDocument();
gnomeFile.Load(file);
// Get list of Gnomes
XmlNodeList nodes = gnomeFile.SelectNodes(string.Format("/GnomeArmy/Gnome"));
Gnome gnome = null;
foreach (XmlNode node in nodes)
{
gnome = new Gnome();
// General Attributes
gnome.Name = node["Name"].InnerText;
gnome.Colour = node["Colour"].InnerText;
XmlNodeList children = node.SelectSingleNode("/GnomeArmy/Gnome/Children").ChildNodes;
foreach (XmlNode child in children)
{
if (child.Name == "Child")
{
gnome.Children = gnome.Children + " " + child.InnerText;
}
}
gnomeList.Add(gnome);
}
return gnomeList;
}
XML:
<GnomeArmy>
<Gnome>
<Name>Harry</Name>
<Colour>Blue</Colour>
<Children>
<Child>Jessica</Child>
<Child>Nick</Child>
</Children>
</Gnome>
<Gnome>
<Name>Mathew</Name>
<Colour>Red</Colour>
<Children>
<Child>Lisa</Child>
<Child>James</Child>
</Children>
</Gnome>
</GnomeArmy>
Upvotes: 2
Views: 159
Reputation: 1012
Turns out, this was the problem as stated by @JonPall:
XmlNodeList children = node.SelectSingleNode("/GnomeArmy/Gnome/Children").ChildNodes;
The part of that statement which was causing the problem was "/GnomeArmy/", the / resulted in the SelectSingleNode going to the top of the XML Doc, remove the "/GnomeArmy" and "/" and it works :)
XmlNodeList children = node.SelectSingleNode("Gnome/Children").ChildNodes;
Credit to my lecturer and to @JonPall for highlighting the line of code.
Upvotes: 0
Reputation: 31576
Use Linq to handle the nodes and children (sub) nodes accordingly via their Descendants
var xdoc = XDocument.Parse(@"
<GnomeArmy>
<Gnome><Name>Harry</Name><Colour>Blue</Colour>
<Children><Child>Jessica</Child><Child>Nick</Child></Children>
</Gnome>
<Gnome><Name>Mathew</Name><Colour>Red</Colour>
<Children><Child>Lisa</Child><Child>James</Child></Children>
</Gnome>
</GnomeArmy>");
Console.WriteLine (
xdoc.Descendants("Gnome")
.Select (parent => string.Format("{0} has these kids {1}",
parent.Descendants("Name").First().Value,
string.Join(", ", parent.Descendants("Child")
.Select (child => child.Value))
)
));
Results from WriteLine
Harry has these kids Jessica, Nick
Mathew has these kids Lisa, James
Upvotes: 1
Reputation: 101680
Try using LINQ to XML
List<Gnome> gnomes = XDocument.Load("path")
.Descendants("Gnome")
.Select(g => new Gnome {
Name = (string)g.Element("Name"),
Colour = (string)g.Element("Colour"),
Childrens = g.Element("Children")
.Elements("Child")
.Select(x => new Children { Name = (string)x)).ToList());
I store Childrens
into a List
, you can change it,if you want just concatenate children names, you can use string.Join
for that:
Childrens = string.Join(" ",g.Element("Children")
.Elements("Child")
.Select(x => (string)x));
Upvotes: 2
Reputation: 814
Your problem is this line:
XmlNodeList children = node.SelectSingleNode("/GnomeArmy/Gnome/Children").ChildNodes;
You are selecting the same node over and over.
Upvotes: 1