Linq-to-Xml 如何将对象转换为字符串?

发布于 2024-08-19 02:30:45 字数 690 浏览 8 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放我走吧 2024-08-26 02:30:45

厄蒙,你是对的。

快速访问 Reflector* 显示 XElement 并且朋友将尝试将值转换为所有“简单”类型,然后调用适当的 XmlConvert.ToString() 方法。

这是(稍作编辑的)摘录,用于说明:

if (value is string)
{
    str = (string) value;
}
else if (value is double)
{
    str = XmlConvert.ToString((double) value);
}
// ...

如果对象不是“简单”类型,则将使用 obj.ToString()。

这非常好,因为这几乎是唯一正确的方法。

当然,你通常会对任何基于“今天就是这样”的事情持保留态度。事实上,微软有一天可能会改变他们的做法。我们不在乎——重要的是语义是一成不变的。

*(为了方便起见,始终提供产品链接。除了作为间接客户之外,我与该产品或其制造商没有任何关系。)

更新:
介绍 Microsoft LINQ 中,Pialorsi 和 Russo 从另一方面证实了这一点方程(提取值并转换它们),第 172 页:

[当将 XElement 转换为给定类型(如 Decimal)时] ...各种显式 [cast] 运算符重载在内部使用 System.Xml 中的 XmlConvert Parse .NET 类型的方法。

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 appropriate XmlConvert.ToString() method.

Here's an (slightly edited) excerpt, for illustration:

if (value is string)
{
    str = (string) value;
}
else if (value is double)
{
    str = XmlConvert.ToString((double) value);
}
// ...

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:

[when casting an XElement to a given type like Decimal] ...the various Explicit [cast] operator overloads internally use XmlConvert from System.Xml or Parse methods of .NET types.

金兰素衣 2024-08-26 02:30:45

虽然我找不到支持这一点的 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) the System.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文