2011-12-13 19 views
10

Jak sprawdzić, czy węzeł ma określony atrybut, czy nie.Sprawdź, czy węzeł Xml ma atrybut

Co zrobiłem to:

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))) 
     { 

     } 
     } 
    } 
    } 

Czy istnieje funkcja, aby sprawdzić, że dana cecha występuje, czy nie?

+3

Słowo to "sprawdź", a nie "chk". – Oded

Odpowiedz

13

Nie, nie sądzę, istnieje metoda w klasie XmlTextReader który może powiedzieć, czy dany atrybut istnieje, czy nie.

Można zrobić jedną rzecz, by sprawdzić

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

ponieważ MSDN mówi o GetAttribute metoda

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

Czy zaproponujesz inną klasę, która ma metodę sprawdzania, czy dany atrybut istnieje, czy nie. –

+0

Nie według mojej wiedzy. Nie znam żadnej metody zwracającej wartość bool o tym, czy atrybut istnieje, czy nie. Wszystko, co wiem, zwraca wartość null, jeśli atrybut nie istnieje –

+0

wielkie dzięki za pomoc –

2

Wypróbuj LINQ-XML (zapytanie poniżej może wymagać drobnych poprawek, ponieważ nie będę mieć XML używasz)

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

Znalazłem to: http://csharpmentor.blogspot.co.uk/2009/05/safely-retrive-attribute-from-xml-node.html

można przekonwertować XmlNode do XmlElement następnie użyj Metoda HasAttribute do sprawdzenia. Po prostu próbowałem i działa - bardzo przydatne.

Niestety nie jest to przykład przy użyciu kodu - spieszę się, ale mam nadzieję, że pomoże to przyszłym pytającym!

+3

Ta odpowiedź jest rażąco niedoceniana. Ten link również warto sprawdzić. +1 – Robino

+2

+1 ode mnie też. Dało mi to pomysł następującego konstruktu do użycia w wywołaniu metody, które potrzebuje łańcucha lub pustego, jeśli atrybut istnieje: '(węzeł jako XmlElement) .HasAttribute (" name2 ")? node.Attributes ["name2"]. Wartość: String.Empty' – St0fF

0

// Sprawdź wartość elementu XML, jeśli istnieje używając XmlReader

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

      if (xmlReader.ReadToFollowing("XMLNODE")) 

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

Biorąc refernce z odpowiedzią Harish, w następujący kod pracował dla mnie

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

Jeśli ktoś nie jest za pomocą czytnika i po prostu XmlDocument spróbuj 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 
} 
Powiązane problemy