I tried with Reader.Depth but the function returns completely false numbers. The XML-file is extremely huge, so here´s just a little example:
Node1
Node1.2
Node1.2.1
Node1.2.1.1
...........
Node1.2.2
Node2.2
Noce2
You got the idea.
So how can I get the real depth of every node? Reader.depth does not work right, it always returns "2", no matter which node I use it on.
Here´s the code snippet:
while (reader2.Read())
{
if (reader2.NodeType == XmlNodeType.Element)
{
switch (reader2.Name)
{
case "the_node":
the_node = new My_Node();
list_myNodes.Add(the_node);
the_node.howdeep = reader2.Depth;
break;
case "name":
[......]
case "argument":
[......]
}
}
}
Later, I get all the right values, name is right, argument is right, all the others are right... only the depth is wrong
Did u try using XMLTextReader ?
You can find the MSDN documentation here !
Additionally see the code snippet from the MSDN documentation below.
using System;
using System.IO;
using System.Xml;
public class Sample{
public static void Main(){
// Create the XML fragment to be parsed.
string xmlFrag =
@"<book>
<misc>
<style>paperback</style>
<pages>240</pages>
</misc>
</book>";
// Create the XmlNamespaceManager.
NameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
// Create the XmlParserContext.
XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);
// Create the reader.
XmlTextReader reader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context);
// Parse the XML and display each node.
while (reader.Read()){
switch (reader.NodeType){
case XmlNodeType.Element:
Console.Write("{0} {1},{2} ", reader.Depth, reader.LineNumber, reader.LinePosition);
Console.WriteLine("<{0}>", reader.Name);
break;
case XmlNodeType.Text:
Console.Write("{0} {1},{2} ", reader.Depth, reader.LineNumber, reader.LinePosition);
Console.WriteLine(" {0}", reader.Value);
break;
case XmlNodeType.EndElement:
Console.Write("{0} {1},{2} ", reader.Depth, reader.LineNumber, reader.LinePosition);
Console.WriteLine("</{0}>", reader.Name);
break;
}
}
// Close the reader.
reader.Close();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.