Raed Sawalha wrote:
I have following XML:
How can I find the depth of XML?
in other word max level of the XML
<Root> ----- level 0
<Name>----- level 1
<FirstName>name</FirstName> ---- level 2
<LasName>lname</LastName>
<MiddleName>mName<MiddleName>
</Name>
<Information>
<Info1>
<Info2> --- level 3
<Info3>asdasd</Info> -- level 4
</Info2>
</Info1>
</Information>
</Root>
XmlTextReader has a Depth property which you could store, however note
that with that approach the text node inside of the <Info3> element is
on level (or depth) 5:
XmlTextReader xmlReader = new XmlTextReader(@"test2005060501.xml");
int maxDepth = 0;
while (xmlReader.Read()) {
if (xmlReader.Depth > maxDepth) {
maxDepth = xmlReader.Depth;
}
Console.WriteLine(
"Node type: {0}, node name: {1}, node depth: {2}.", xmlReader.NodeType,
xmlReader.Name, xmlReader.Depth);
}
Console.Write("Maximum depth is: {0}.", maxDepth);
If you do not want to count text nodes or comment nodes you need to
check the NodeType and only change maxDepth for the node types you are
looking for.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/