如何检查XmlAttributeCollection中是否存在属性?

Jun*_*r M 18 c# 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...
  }
Run Code Online (Sandbox Code Playgroud)

那么,检查每个节点中是否存在属性的最佳方法是什么?可以使用node.Attribute["IsCapital"]!=null吗?

Ode*_*ded 37

只需使用索引器 - 如果该属性不存在,索引器将返回null:

bool isCapital = nodes.Attributes["IsCapital"] != null;
Run Code Online (Sandbox Code Playgroud)

这是记录在案的XmlAttributeCollection.ItemOfProperty (String).

XmlAttribute具有指定名称.如果该属性不存在,则返回此属性null.