2011-12-13 124 views
10

如何检查节点是否具有特定属性。检查Xml节点是否具有属性

我所做的是:

string referenceFileName = xmlFileName; 
XmlTextReader textReader = new XmlTextReader(referenceFileName); 

while (textReader.Read()) 
{ 
    XmlNodeType nType = textReader.NodeType; 

    // if node type is an element 
    if (nType == XmlNodeType.Element) 
    { 
    if (textReader.Name.Equals("Control")) 
    { 
     if (textReader.AttributeCount >= 1) 
     { 
     String val = string.Empty; 
     val = textReader.GetAttribute("Visible"); 
     if (!(val == null || val.Equals(string.Empty))) 
     { 

     } 
     } 
    } 
    } 

是否有任何函数来检查一个给定的属性存在与否?

+3

这个词是“检查”,而不是“chk”。 – Oded

回答

13

不,我不认为这是在XmlTextReader类的任何方法它可以告诉你某个特定属性是否存在。

你可以做一两件事来检查

if(null == textReader.GetAttribute("Visible")) 
{ 
    //this means attribute doesn't exist 
} 

因为MSDN说,大约GetAttribute方法

Return the value of the specified attribute. If the attribute is not found, 
a null reference (Nothing in Visual Basic) is returned. 
+0

你会建议任何其他具有方法来检查某个特定属性是否存在的类。 –

+0

不是我所知。我不知道任何方法返回关于属性是否存在的布尔值。我知道的所有返回null如果属性不存在 –

+0

非常感谢您的帮助 –

2

试用LINQ到XML(以下查询可能需要小的修改,因为我没有XML你正在使用)

XDocument xdoc = XDocument.Load("Testxml.xml"); 

// It might be that Control element is a bit deeper in XML document 
// hierarchy so if you was not able get it work please provide XML you are using 
string value = xdoc.Descendants("Control") 
        .Where(d => d.HasAttributes 
           && d.Attribute("Visible") != null 
           && !String.IsNullOrEmpty(d.Attribute("Visible").Value)) 
        .Select(d => d.Attribute("Visible").Value) 
        .Single(); 
8

发现这一点:http://csharpmentor.blogspot.co.uk/2009/05/safely-retrive-attribute-from-xml-node.html

您可以使用XMLNode转换成XmlElement的再使用HasAttribute方法来检查。我只是试过它,它的工作原理 - 非常有用。

对不起,它不是一个使用你的代码的例子 - 我匆忙,但希望它有助于未来askers!

+3

这个答案是严重低估。这个链接也值得一试。 +1 – Robino

+2

也是我的+1。它给了我以下构造的想法,用于需要字符串的方法调用,或者如果属性存在,则为空: '(node as XmlElement).HasAttribute(“name2”)? node.Attributes [“name2”]。Value:String.Empty' – St0fF

0

//检查XML元素的值是否存在使用的XmlReader

 using (XmlReader xmlReader = XmlReader.Create(new StringReader("XMLSTRING"))) 
     { 

      if (xmlReader.ReadToFollowing("XMLNODE")) 

      { 
       string nodeValue = xmlReader.ReadElementString("XMLNODE");     
      } 
     }  
0

从Harish的答案以refernce,下面的代码为我工作

XmlTextReader obj =new XmlTextReader("your path"); 
    //include @before"" if its local path 
     while (obj.Read()) { 
     Console.WriteLine(obj.GetAttribute("name")); 
     obj.MoveToNextAttribute(); 
     } 
0

如果有人不使用阅读器,只是一个XmlDocument尝试XmlAttributeCollection/XmlAttribute

XmlDocument doc = new XmlDocument(); 
try{ 
doc.Load(_indexFile); 
    foreach(XmlNode node in doc.DocumentElement.ChildNodes){ 
     XmlAttributeCollection attrs = node.Attributes; 
     foreach(XmlAttribute attr in attrs){ 
      Console.WriteLine(node.InnerText + ": " + attr.Name + " - " + attr.Value); 
      if(attr.Name == "my-amazing-attribute") 
      { 
       //Do something with attribute 
      } 
     } 
    } 
} 
} catch (Exception ex) { 
    //Do something with ex 
}