XmlTextReader.Depth Property gets the depth of the current node in the XML document.
Imports System
Imports System.IO
Imports System.Xml
Imports Microsoft.VisualBasic
public class Sample
public shared sub Main()
Dim xmlFrag as string = "<book><misc><style>paperback</style><pages>240</pages></misc></book>"
Dim nt as NameTable = new NameTable()
Dim nsmgr as XmlNamespaceManager = new XmlNamespaceManager(nt)
Dim context as XmlParserContext = new XmlParserContext(nothing, nsmgr, nothing, XmlSpace.None)
Dim reader as XmlTextReader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context)
while (reader.Read())
select case reader.NodeType
case XmlNodeType.Element:
Console.Write("{0} {1},{2} ", reader.Depth, reader.LineNumber, reader.LinePosition)
Console.WriteLine("<{0}>", reader.Name)
case XmlNodeType.Text:
Console.Write("{0} {1},{2} ", reader.Depth, reader.LineNumber, reader.LinePosition)
Console.WriteLine(" {0}", reader.Value)
case XmlNodeType.EndElement:
Console.Write("{0} {1},{2} ", reader.Depth, reader.LineNumber, reader.LinePosition)
Console.WriteLine("</{0}>", reader.Name)
end select
End while
reader.Close()
End sub
End class
Related examples in the same category