XElement.Load(TextReader, LoadOptions) loads an XElement from a TextReader
Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.XPath
Public Class MainClass
Public Shared Sub Main()
Dim sr As TextReader
Dim whiteSpaceNodes As Integer
sr = New StringReader("<Root> <Child> </Child> </Root>")
Dim xmlTree1 As XElement = XElement.Load(sr, LoadOptions.None)
sr.Close()
whiteSpaceNodes = xmlTree1 _
.DescendantNodesAndSelf() _
.OfType(Of XText)() _
.Where(Function(ByVal tNode As XNode) tNode.ToString().Trim().Length = 0) _
.Count()
Console.WriteLine(whiteSpaceNodes)
sr = New StringReader("<Root> <Child> </Child> </Root>")
Dim xmlTree2 As XElement = XElement.Load(sr, LoadOptions.PreserveWhitespace)
sr.Close()
whiteSpaceNodes = xmlTree2 _
.DescendantNodesAndSelf() _
.OfType(Of XText)() _
.Where(Function(ByVal tNode As XNode) tNode.ToString().Trim().Length = 0) _
.Count()
Console.WriteLine(whiteSpaceNodes)
End Sub
End Class
Related examples in the same category