XDocument.Parse Method creates a new XDocument from a string, optionally preserving white space
Imports System Imports System.Xml Imports System.Xml.XPath Public Class MainClass Public Shared Sub Main() Dim str As String = _ "<?xml version= '1.0'?>" & Environment.NewLine & _ "<!-- comment at the root level -->" & Environment.NewLine & _ "<Root>" & Environment.NewLine & _ " <Child>Content</Child>" & Environment.NewLine & _ "</Root>" Dim doc1 As XDocument = XDocument.Parse(str, LoadOptions.PreserveWhitespace) Console.WriteLine("nodes when preserving whitespace: {0}", doc1.DescendantNodes().Count()) Dim doc2 As XDocument = XDocument.Parse(str, LoadOptions.None) Console.WriteLine("nodes when not preserving whitespace: {0}", doc2.DescendantNodes().Count()) End Sub End Class