Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从节点xml获取属性_C#_.net_Linq To Xml - Fatal编程技术网

C# 如何从节点xml获取属性

C# 如何从节点xml获取属性,c#,.net,linq-to-xml,C#,.net,Linq To Xml,类似于node.Attributes[“name”].InnerText是好方法吗?您应该使用类的属性: 请注意,该方法返回必须迭代的实例,而不是XAttribute实例。此外,这些是方法而不是索引属性:您需要使用括号而不是方括号来调用它们 XAttribute也不支持InnerText属性,因此您必须改用Value。您可以使用此选项,在属性为null时捕获异常 string attrValue = node.Attributes["name"] == null ? string.Empty :

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

您应该使用类的属性:

请注意,该方法返回必须迭代的实例,而不是
XAttribute
实例。此外,这些是方法而不是索引属性:您需要使用括号而不是方括号来调用它们


XAttribute
也不支持
InnerText
属性,因此您必须改用
Value

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

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

有什么区别?另外,属性[xxx]和属性(xxx)之间有什么区别?@user496949,没有区别,因为
XAttribute
不支持
InnerText
。请参阅我的最新答案。我使用LINQ到XML类型,因为您的问题被标记为这样,也许您还需要其他东西?
string attrValue = node.Attributes["name"] == null ? string.Empty : node.Attributes["name"].Value;