Last post Oct 03, 2011 12:27 PM by Cerberus2589
Oct 03, 2011 11:05 AM|Cerberus2589|LINK
I have an XML file which could have x number of child nodes within child nodes. For example, my XML file may have this format:
Equally though nodeD may not be there on another document or it may have even more child nodes than that (such as nodeE etc). If you think of a namespace heirarchy you'll understand what I'm talking about. How can I loop through all of these nodes no matter
how many there are and retrieve the data out of them?
Oct 03, 2011 11:49 AM|chandrashekar|LINK
Try the following this:
XmlDocument doc = new XmlDocument();
System.Text.StringBuilder dataBuilder = new StringBuilder();
string dataFromXmlNodes = GetDataFromChildNodes(doc.DocumentElement.ChildNodes, dataBuilder);
Here is the method implementation
private static string GetDataFromChildNodes(XmlNodeList elements, StringBuilder builder)
foreach (XmlNode node in elements)
Oct 03, 2011 12:05 PM|Cerberus2589|LINK
Ah so you just get the method to keep calling itself until there are no more child nodes to add? I like the theory I'll give it a try thanks for your answer
Oct 03, 2011 12:08 PM|chandrashekar|LINK
Yes, It will recrusive until the node do not have the child nodes and append the data seperated by comma(,) from the node and return the string.
Oct 03, 2011 12:13 PM|Cerberus2589|LINK
And I suppose because StringBuilder is a reference type not a value type it would keep all the previous values added to it when a new method is called?
Oct 03, 2011 12:24 PM|chandrashekar|LINK
Yes. Did the solution worked? have you tested it?
Oct 03, 2011 12:27 PM|Cerberus2589|LINK
Not yet but I don't see why this shouldn't work so I'll mark your response as the answer.