C# in Depth

Cover of C# in Depth
Order now (3rd edition)

XText creation

Chapter 12: LINQ beyond collections: P346, section 12.3.1

Created: 1/29/2014
Last updated: 1/29/2014
Severity: Language improvement

When describing XText, the book states:

XText is rarely instantiated directly in user code; instead, when a string is used as the content of an element or document, that’s converted into an XText instance.

That's somewhat misleading, as it ignores all the other conversions which implicitly create XText nodes. Basically, any time you create text within an element, it will be a form of XText. For example, consider this code:

new XElement("x", 0)

This creates this XML:

<x>0</x>

The 0 has been specified as an integer literal, but in the object tree there's still an XText object, as you can tell by calling element.Nodes().First().GetType().