为什么 XElement 没有 GetAttributeValue 方法?
有时我想知道某些 API 更改的原因。由于 Google 没有帮助我解决这个问题,也许 StackOverflow 可以。为什么 Microsoft 选择删除 XML 元素上的 GetAttribute
辅助方法?在 System.Xml 世界中,有类似于 MSXML 中的 getAttribute 的 XmlElement.GetAttribute("x")
,两者都返回属性值或缺失时为空字符串。对于 XElement
,有 SetAttributeValue
,但未实现 GetAttributeValue
。
当然,修改逻辑来测试和使用 XElement.Attribute("x").Value
属性并不需要太多工作,但它并不那么方便,并且以一种方式提供实用程序函数 (SetAttributeValue< /code>) 但另一个似乎很奇怪。有谁知道这个决定背后的原因,以便我可以轻松休息,也许可以从中学到一些东西?
Sometimes I'd like to know the reasoning of certain API changes. Since Google hasn't helped me with this question, maybe StackOverflow can. Why did Microsoft choose to remove the GetAttribute
helper method on XML elements? In the System.Xml
world there was XmlElement.GetAttribute("x")
like getAttribute
in MSXML before it, both of which return either the attribute value or an empty string when missing. With XElement
there's SetAttributeValue
but GetAttributeValue
wasn't implemented.
Certainly it's not too much work to modify logic to test and use the XElement.Attribute("x").Value
property but it's not as convenient and providing the utility function one way (SetAttributeValue
) but not the other seems weird. Does anyone out there know the reasons behind the decision so that I can rest easily and maybe learn something from it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该获得如下属性值:
更新:
示例:
等。
请参阅这篇文章:http://www.hanselman.com/blog/ImprovingLINQCodeSmellWithExplicitAndImplicitConversionOperators.aspx。同样的事情也适用于属性。
You are supposed to get attribute value like this:
UPDATE:
Examples:
etc.
See this article: http://www.hanselman.com/blog/ImprovingLINQCodeSmellWithExplicitAndImplicitConversionOperators.aspx. Same thing works for attributes.
不确定具体原因,但使用 C# 扩展方法,您可以自己解决问题。
允许:
Not sure exactly the reason, but with C# extension methods, you can solve the problem yourself.
Allows: