Linq-to-Xml 如何将对象转换为字符串?
Linq-to-Xml 包含许多允许您将任意对象添加到 xml 树的方法。这些对象通过某种方式转换为字符串,但我似乎找不到这种情况如何发生的规范。我所指的转换在 MSDN 中提到(但未指定)。
我碰巧需要这个来实现 javascript 互操作,但这对问题来说并不重要。
Linq to Xml 不仅仅是调用 .ToString()
。首先,它接受 null
元素,其次,它执行 .ToString()
实现不执行的操作:
例如:
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"
其他基本数据类型也受到类似的特殊处理。
那么,有人知道它在做什么以及在哪里进行了描述吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
厄蒙,你是对的。
快速访问 Reflector* 显示
XElement
并且朋友将尝试将值转换为所有“简单”类型,然后调用适当的XmlConvert.ToString()
方法。这是(稍作编辑的)摘录,用于说明:
如果对象不是“简单”类型,则将使用 obj.ToString()。
这非常好,因为这几乎是唯一正确的方法。
当然,你通常会对任何基于“今天就是这样”的事情持保留态度。事实上,微软有一天可能会改变他们的做法。我们不在乎——重要的是语义是一成不变的。
*(为了方便起见,始终提供产品链接。除了作为间接客户之外,我与该产品或其制造商没有任何关系。)
更新:
在介绍 Microsoft LINQ 中,Pialorsi 和 Russo 从另一方面证实了这一点方程(提取值并转换它们),第 172 页:
Earmon, you are correct.
A quick visit with Reflector* reveals that
XElement
and friends will try to cast the value to all the 'simple' types, and then call an appropriateXmlConvert.ToString()
method.Here's an (slightly edited) excerpt, for illustration:
If the object is not a 'simple' type, then obj.ToString() will be used.
This is very nice, because that's almost the only right way to do it.
Of course, you normally want to take with a grain of salt anything based on "that's how it is today". Indeed, MS might one day change how they do it. We don't care -- what matters is that the semantics are carved in stone.
*(Product links are always provided for convenience. I have no relationship with the product or its manufacturer, other than being an indirect customer.)
Update:
In Introducing Microsoft LINQ, Pialorsi and Russo confirm this for the other side of the equation (extracting values and casting them), on page 172:
虽然我找不到支持这一点的 MSDN 文档,但当您执行诸如
new XElement("bla",false)
之类的操作时,System.Xml.XmlConvert
类将用于以非本地化方式(反)序列化数据。换句话说,如果其他人需要了解将(非 xml)对象添加到 xml 树中时 linq to xml 到底执行什么操作,请查看 System.Xml.XmlConvert。
Although I can't find MSDN documentation to support this, when you do something like
new XElement("bla",false)
theSystem.Xml.XmlConvert
class is uses to (de-)serialize the data in a non-localized fashion.In other words, if anyone else needs to know what exactly linq to xml does when you add a (non-xml) object into an xml tree, look at
System.Xml.XmlConvert
.