Export (0) Print
Expand All

White Space in XML Literals

The Visual Basic compiler incorporates only the significant white space characters from an XML literal when it creates a LINQ to XML object. The insignificant white space characters are not incorporated.

White space characters in XML literals are significant in only three areas:

  • When they are in an attribute value.

  • When they are part of an element's text content and the text also contains other characters.

  • When they are in an embedded expression for an element's text content.

Otherwise, the compiler treats white space characters as insignificant and does not include then in the LINQ to XML object for the literal.

To include insignificant white space in an XML literal, use an embedded expression that contains a string literal with the white space.


If the xml:space attribute appears in an XML element literal, the Visual Basic compiler includes the attribute in the XElement object, but adding this attribute does not change how the compiler treats white space.

The following example contains two XML elements, outer and inner. Both elements contain white space in their text content. The white space in the outer element is insignificant because it contains only white space and an XML element. The white space in the inner element is significant because it contains white space and text.

Dim example As XElement = <outer>
                                  Inner text 


When run, this code displays the following text.

                                          Inner text

Community Additions

© 2015 Microsoft