2012-08-16 15 views
5

Ho bisogno di leggere tutto il Child Nodes del mio tag <Imovel>, il problema è che ho più di 1 (uno) tag <Imovel> nel mio file XML e la differenza tra ogni tag <Imovel> è un attributo chiamato ID.Leggere tutti i nodi figlio XML di ciascun nodo specifico

Questo è un esempio

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

ho bisogno di leggere tutte le etichette di ogni <Imovel> tag, e alla fine di ogni convalida che faccio nel mio tag <Imovel> Ho bisogno di fare un altro convalida.

Quindi, penso che ho bisogno di fare 2 (due) o un foreachfor e foreach, non capisco molto bene di LINQ ma seguo il mio campione

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; 
    } 
} 

ma non funziona molto beh, nella mia istruzione foreach perché il mio tag <Picture>, il suo tag padre non ha un attributo ID.

Qualcuno può aiutarmi a fare questo metodo?

+0

'ma non funziona molto bene, a mio foreach statement.' si può spiegare cosa si intende con questo? –

+0

Sì, questo è perché il padre del mio tag '' non ha un attributo ID –

risposta

7

Si dovrebbe essere in grado di fare questo con due affermazioni 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

provare questo. System.Xml.XPath aggiungerà selettori xpath a XElement. La ricerca di elementi è molto più veloce e semplice con xpath.

Non è necessario XmlReader & XDocument per caricare il file.

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); 
} 

si prega di includere

System.Xml.XPath; 
Problemi correlati