XDocument.Load (XmlReader, LoadOptions) loads an XElement from an XmlReader, setting the base URI, and retaining line information.
Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.XPath
Public Class MainClass
Public Shared Sub Main()
Dim markup As String = _
"<Root>" & Environment.NewLine & _
" <Child>" & Environment.NewLine & _
" <GrandChild/>" & Environment.NewLine & _
" </Child>" & Environment.NewLine & _
"</Root>"
Using nodeReader As XmlReader = XmlReader.Create(New StringReader(markup))
nodeReader.MoveToContent()
Dim xRoot As XDocument = XDocument.Load(nodeReader, LoadOptions.SetLineInfo)
For Each e As XElement In xRoot.Elements("Root").DescendantsAndSelf()
Console.WriteLine("{0}{1}{2}", _
("".PadRight(e.Ancestors().Count() * 2) & e.Name.ToString()).PadRight(20), _
(DirectCast(e, IXmlLineInfo)).LineNumber.ToString().PadRight(5), _
(DirectCast(e, IXmlLineInfo)).LinePosition)
Next
End Using
End Sub
End Class
Related examples in the same category