簡體   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