[英]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.