[英]How to get the attributes from a node xml
是类似于node.Attributes [“ name”]。InnerText的好方法吗?
You should use the Value property of the XAttribute class: 您应该使用XAttribute类的Value属性:
string attrValue = element.Attribute("name").Value;
Note that the Attributes() method returns an IEnumerable<XAttribute>
that you have to iterate over, instead of an XAttribute
instance. 请注意, Attributes()方法返回的是您必须迭代的IEnumerable<XAttribute>
,而不是XAttribute
实例。 Moreover, those are methods and not indexed properties: you need to call them using parenthesis and not square brackets. 而且,这些是方法,而不是索引属性:您需要使用括号而不是方括号来调用它们。
XAttribute
also doesn't support an InnerText
property, so you'll have to use Value
instead. XAttribute
还不支持InnerText
属性,因此您必须使用Value
。
您可以使用此选项,以在属性为null时捕获异常
string attrValue = node.Attributes["name"] == null ? string.Empty : node.Attributes["name"].Value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.