2011-12-13 16 views
10

मैं कैसे जांचूं कि नोड में विशेष विशेषता है या नहीं।जांचें कि क्या एक्सएमएल नोड में एक विशेषता है

मैं क्या है:

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 कक्षा में कोई भी तरीका है जो आपको बता सकता है कि कोई विशेष विशेषता मौजूद है या नहीं।

आप GetAttribute के बारे में विधि

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

जाँच करने के लिए एक बात कर सकते हैं क्योंकि MSDN कहते

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

क्या आप किसी भी अन्य वर्ग का सुझाव देंगे जिसमें यह जांचने की विधि है कि कोई विशेष विशेषता मौजूद है या नहीं। –

+0

मेरे ज्ञान में नहीं। मुझे कोई विधि नहीं पता है जो गुण मौजूद है या नहीं, इसके बारे में बूल मान देता है। यदि मैं विशेषता नहीं है तो सभी को रिटर्न शून्य पता है –

+0

आपकी मदद के लिए बहुत बहुत धन्यवाद –

2

आजमाएँ LINQ-टू-एक्सएमएल (नीचे क्वेरी मामूली सुधारों की आवश्यकता हो सकती हूँ के बाद से मैं एक्सएमएल प्रयोग कर रहे हैं की जरूरत नहीं)

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

आप एक XmlElement को XmlNode में बदल सकते हैं तो का उपयोग जांच करने के लिए हैसएट्रिब्यूट विधि। मैंने अभी कोशिश की और यह काम करता है - बहुत उपयोगी।

क्षमा करें, यह आपके कोड का उपयोग करके एक उदाहरण नहीं है - मैं जल्दबाजी में हूं लेकिन उम्मीद है कि यह भविष्य के पूछताछ करने वालों की मदद करेगा!

+3

यह उत्तर पूरी तरह से अंतर्निहित है। यह लिंक भी जांचने लायक है। मेरे द्वारा +1 – Robino

+2

+1 भी। यह मुझे विधि-कॉल में उपयोग करने के लिए निम्न निर्माण का विचार देता है, यदि विशेषता मौजूद है तो स्ट्रिंग या रिक्त की आवश्यकता होती है: '(एक्सएमएलएलमेंट के रूप में नोड) .असएट्रिब्यूट (" name2 ")? नोड। गुण ["name2"]। मूल्य: स्ट्रिंग। लक्षण ' – St0fF

0

// एक्सएमएल तत्व मूल्य की जाँच करें अगर XmlReader

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

      if (xmlReader.ReadToFollowing("XMLNODE")) 

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

हरीश के जवाब से 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 
} 
संबंधित मुद्दे