XmlElement test=new XmlElement();
  test.Attributes.Count;//接点数目
if(test.Attributes["arr1"].Value==null)
{
//没有属性
}
else //有属性

解决方案 »

  1.   

    using System.Xml;XmlDocument xmldoc = new XmlDocument();
    xmldoc.Load("YourXMLFilePath.xml");
    XmlNode node = xmldoc.SelectSingleNode("//node1[@arr1]");
    if (node != null)
    {
    //exists
    }
      

  2.   

    XmlNode  test=  new XmlNode();
    test.Attributes.Count; //接点数目
    if(test.Attributes["arr1"].Value==null)
    {
    //没有属性
    }
    else //有属性
      

  3.   

    也可以在xpath里面指定
    XmlDocument xmlDom=new XmlDocument();
    xmlDom.Load("myxmlfile.xml");
    XmlNodeList nc=xmlDom.SelectNodes("YourPath/MyNode[@Arr1]");//找出YourPath下面具有Attr1属性的MyNode节点
      

  4.   

    谢谢两位!
    再问一下,如果节点node1中没有arr1属性,我用下面语句:
     _strTemp = node1.Attributes["arr1"].Value;
    _strTemp是得到了null值呢,还是抛出一个异常,出错。_strTemp是string类型的,如果是int类型的又会怎么样呢。
      

  5.   

    应该会有空引用的异常,属性可以动态加,如下:XmlDocument doc = new XmlDocument();

    doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-5-5'>" +
    "<title>Pride And PreJudice</title>"+
    "</book>");

    XmlElement root = doc.DocumentElement;

    //add a new attributes
    root.SetAttribute("genre", "urn:samples", "novel");

    XmlAttribute attr = doc.CreateAttribute("publisher");
    attr.Value = "WorldWide Publishing";

    root.SetAttributeNode(attr);
      

  6.   

    string strTemp = "";
    if (node1.Attributes["arr1"] != null)
    {
      strTemp = node1.Attributes["arr1"].Value;
    }