繁体   English   中英

如何从节点xml获取属性

[英]How to get the attributes from a node xml

是类似于node.Attributes [“ name”]。InnerText的好方法吗?

您应该使用XAttribute类的Value属性:

string attrValue = element.Attribute("name").Value;

请注意, Attributes()方法返回的是您必须迭代的IEnumerable<XAttribute> ,而不是XAttribute实例。 而且,这些是方法,而不是索引属性:您需要使用括号而不是方括号来调用它们。

XAttribute还不支持InnerText属性,因此您必须使用Value

您可以使用此选项,以在属性为null时捕获异常

string attrValue = node.Attributes["name"] == null ? string.Empty : node.Attributes["name"].Value;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM