簡體   English   中英

如何檢查XmlAttributeCollection中是否存在屬性?

[英]How can I check if an attribute exist in XmlAttributeCollection?

我在MSDN上檢查XmlNode.Attributes主題有關檢查XmlNode屬性是否存在的方法。 那么,沒有關於如何檢查項目的示例。

我有類似的東西:

  //some code here...

  foreach (XmlNode node in n.SelectNodes("Cities/City"))
  {
        //is there some method to check an attribute like
        bool isCapital = node.Attributes.Exist("IsCapital");

        //some code here...
  }

那么,檢查每個節點中是否存在屬性的最佳方法是什么? 是否可以使用node.Attribute["IsCapital"]!=null

只需使用索引器 - 如果該屬性不存在,索引器將返回null

bool isCapital = nodes.Attributes["IsCapital"] != null;

這在XmlAttributeCollection.ItemOfProperty (String)

具有指定名稱的XmlAttribute 如果該屬性不存在,則此屬性返回null

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM