Bagikan melalui


White Space in XML Literals (Visual Basic)

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.

Significant and Insignificant White Space

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.

Note

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.

Examples

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> 
                                  Inner text 
                              </inner>
                          </outer>

Console.WriteLine(example)

When run, this code displays the following text.

<outer>
  <inner>
                                          Inner text
                                      </inner>
</outer>

See Also

Other Resources

Creating XML in Visual Basic