How does Linq-to-Xml convert objects to strings?

Posted by Eamon Nerbonne on Stack Overflow See other posts from Stack Overflow or by Eamon Nerbonne
Published on 2010-01-25T13:01:06Z Indexed on 2010/03/12 19:57 UTC
Read the original article Hit count: 421

Filed under:
|
|
|
|

Linq-to-Xml contains lots of methods that allow you to add arbitrary objects to an xml tree. These objects are converted to strings by some means, but I can't seem to find the specification of how this occurs. The conversion I'm referring to is mentioned (but not specified) in MSDN.

I happen to need this for javascript interop, but that doesn't much matter to the question.

Linq to Xml isn't just calling .ToString(). Firstly, it'll accept null elements, and secondly, it's doing things no .ToString() implementation does:

For example:

new XElement("elem",true).ToString() == "<elem>true</elem>"
//but...
true.ToString() == "True" //IIRC, this is culture invariant, but in any case...
true.ToString(CultureInfo.InvariantCulture) == "True"

Other basic data types are similarly specially treated.

So, does anybody know what it's doing and where that's described?

© Stack Overflow or respective owner

Related posts about linq-to-xml

Related posts about tostring