2012-08-16 16 views
5

मैं पढ़ने के लिए मेरे सभी <Imovel> टैग की Child Nodes, समस्या यह है कि मैं अपने XML फ़ाइल में 1 से अधिक (एक) <Imovel> टैग, और प्रत्येक <Imovel> टैग के बीच का अंतर है की जरूरत है पढ़ें आईडी नामक एक विशेषता है।प्रत्येक विशिष्ट नोड के सभी XML बच्चे नोड्स

यह एक उदाहरण

<Imoveis> 
    <Imovel id="555"> 
     <DateImovel>2012-01-01 00:00:00.000</DateImovel> 
     <Pictures> 
      <Picture> 
       <Path>hhhhh</Path> 
      </Picture> 
     </Pictures> 
     // Here comes a lot of another tags 
    </Imovel> 
    <Imovel id="777"> 
     <DateImovel>2012-01-01 00:00:00.000</DateImovel> 
     <Pictures> 
      <Picture> 
       <Path>tttt</Path> 
      </Picture> 
     </Pictures> 
     // Here comes a lot of another tags 
    </Imovel> 
</Imoveis> 

मैं हर <Imovel> टैग के सभी टैग को पढ़ने की जरूरत है, और प्रत्येक मान्यता है कि मैं अपने <Imovel> टैग में क्या के अंत में मैं एक और मान्यकरण करना होगा।

तो, मुझे लगता है कि मैं 2 (दो) foreach या एक for और एक foreach क्या करने की जरूरत है, मैं बहुत अच्छी तरह से LINQ के बारे में समझ में नहीं आता, लेकिन पालन मेरी नमूना

XmlReader rdr = XmlReader.Create(file); 
XDocument doc2 = XDocument.Load(rdr); 
ValidaCampos valida = new ValidaCampos(); 

//// Here I Count the number of `<Imovel>` tags exist in my XML File       
for (int i = 1; i <= doc2.Root.Descendants().Where(x => x.Name == "Imovel").Count(); i++) 
{ 
    //// Get the ID attribute that exist in my `<Imovel>` tag 
    id = doc2.Root.Descendants().ElementAt(0).Attribute("id").Value; 

    foreach (var element in doc2.Root.Descendants().Where(x => x.Parent.Attribute("id").Value == id)) 
    { 
     String name = element.Name.LocalName; 
     String value = element.Value; 
    } 
} 

लेकिन बहुत से काम नहीं करता ठीक है, मेरे foreach कथन में क्योंकि मेरा <Picture> टैग, उसके मूल टैग में आईडी विशेषता नहीं है।

कोई मुझे इस विधि को करने में मदद कर सकता है?

+0

'लेकिन मेरे foreach statement.' में बहुत अच्छी तरह से काम नहीं करता है, तो आप आप इस से क्या मतलब है व्याख्या कर सकते हैं? –

+0

हां, यह मेरे ' 'के माता-पिता का कारण है, टैग में आईडी विशेषता नहीं है –

उत्तर

7

आप दो foreach बयान के साथ यह करने के लिए सक्षम होना चाहिए:

foreach(var imovel in doc2.Root.Descendants("Imovel")) 
{ 
    //Do something with the Imovel node 
    foreach(var children in imovel.Descendants()) 
    { 
    //Do something with the child nodes of Imovel. 
    } 
} 
1

इस प्रयास करें। System.Xml.XPath XElement में xpath चयनकर्ता जोड़ देगा। एक्सपैथ के साथ तत्व ढूंढना बहुत तेज़ और सरल है।

आपको फ़ाइल लोड करने के लिए XmlReader & XDocument की आवश्यकता नहीं है।

XElement root = XElement.Load("test.xml"); 

foreach (XElement imovel in root.XPathSelectElements("//Imovel")) 
{ 
    foreach (var children in imovel.Descendants()) 
    { 
    String name = children.Name.LocalName; 
    String value = children.Value; 

    Console.WriteLine("Name:{0}, Value:{1}", name, value); 
    } 

    //use relative xpath to find a child element 
    XElement picturePath = imovel.XPathSelectElement(".//Pictures/Picture/Path"); 
    Console.WriteLine("Picture Path:{0}", picturePath.Value); 
} 

शामिल करें

System.Xml.XPath; 
संबंधित मुद्दे