el.Attribute(“...”) 和 el.Attribute(XName.Get(“...”)) 之间有区别吗?

发布于 2024-11-26 23:58:41 字数 281 浏览 2 评论 0原文

在我们的生产代码中,我看到使用显式 XName.Get 调用读取 XML 属性:

var name = element.Attribute (XName.Get ("name"));

我过去总是将字符串传递给 Attribute

var name = element.Attribute ("name");

这更具可读性,但我想知道逻辑或性能是否有任何差异。

In our production code, I've seen XML attributes being read using explicit XName.Get call:

var name = element.Attribute (XName.Get ("name"));

I used to always pass a string to Attribute:

var name = element.Attribute ("name");

This is more readable but I wonder if there is any difference in logic or performance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

罪#恶を代价 2024-12-03 23:58:41

没有任何区别。
XName 具有来自调用 XName.Getstring 的隐式转换。

你可以在源码中看到这一点:

/// <summary>
/// Converts a string formatted as an expanded XML name ({namespace}localname) to an XName object. 
/// </summary>
/// <param name="expandedName">A string containing an expanded XML name in the format: {namespace}localname.</param> 
/// <returns>An XName object constructed from the expanded name.</returns> 
[CLSCompliant(false)]
public static implicit operator XName(string expandedName) { 
    return expandedName != null ? Get(expandedName) : null;
}

There is no difference whatsoever.
XName has an implicit cast from string which calls XName.Get.

You can see this in the source:

/// <summary>
/// Converts a string formatted as an expanded XML name ({namespace}localname) to an XName object. 
/// </summary>
/// <param name="expandedName">A string containing an expanded XML name in the format: {namespace}localname.</param> 
/// <returns>An XName object constructed from the expanded name.</returns> 
[CLSCompliant(false)]
public static implicit operator XName(string expandedName) { 
    return expandedName != null ? Get(expandedName) : null;
}
温柔少女心 2024-12-03 23:58:41

嗯,这有两个部分:

它们是否调用相同的 Attribute 方法?

是的。只有一个 XElement.Attribute方法,带有 XName 参数,这意味着在后一种情况下,您将使用隐式字符串到 XName 转换。

隐式字符串到 XName 转换的效果是否与 XName.Get 相同?

不能保证这一点 - 文档没有提及它。但我没有理由怀疑 SLAks 的分析,即当前的实现是相同的。


就个人而言,我总是要么使用从字符串到XName的转换,要么使用XNamespace和字符串之间的加法运算符来获取XName。我不记得上次明确提到它是什么时候了。

可用的转换是 LINQ to XML 的美妙之处之一 - 在我看来,忽略它们似乎毫无意义。

Well, there are two parts to this:

Are they calling the same Attribute method?

Yes. There's only one XElement.Attribute method, with an XName parameter, which means that in the latter case you are using the implicit string to XName conversion.

Does the implicit string to XName conversion do the same as XName.Get?

This isn't guaranteed - the documentation doesn't mention it. But I have no reason to doubt SLaks' analysis that the current implementation is the same.


Personally I always either use the conversion from string to XName or the addition operator between XNamespace and string to get an XName. I can't remember the last time I referred to it explicitly.

The conversions available are one of the beautiful things about LINQ to XML - it seems pointless to ignore them, IMO.

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