2012-08-16 15 views
5

Potrzebuję przeczytać wszystkie Child Nodes z mojego tagu <Imovel>, problem polega na tym, że mam więcej niż 1 (jeden) znacznik <Imovel> w moim pliku XML i różnicę między każdą etykietą <Imovel> jest atrybutem o nazwie ID.Przeczytaj wszystkie węzły potomne XML każdego konkretnego węzła.

To jest przykład

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

muszę przeczytać wszystkie znaczniki każdego tagu <Imovel>, aw końcu każdego walidacji, które robię w moim <Imovel> tagu muszę zrobić kolejny walidacji.

Tak, myślę, że trzeba zrobić 2 (dwóch) foreach lub for i foreach, nie rozumiem bardzo dobrze o LINQ ale podążać moja próbka

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

Ale nie działa bardzo Cóż, w moim oświadczeniu foreach ponieważ mój tag <Picture>, jej tag macierzysty nie ma atrybutu ID.

Ktoś może mi pomóc w tej metodzie?

+0

'ale nie działa bardzo dobrze, moim foreach statement.' można wyjaśnić, co rozumiemy przez to? –

+0

Tak, to znaczy, że rodzic mojego tagu "" nie ma atrybutu identyfikatora –

Odpowiedz

7

Powinieneś być w stanie to zrobić z dwóch sprawozdań 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

spróbować. System.Xml.XPath doda selektory xpath do XElement. Znalezienie elementów jest znacznie szybsze i prostsze dzięki xpath. Nie trzeba XmlReader & XDocument, aby załadować plik.

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

proszę podać

System.Xml.XPath; 
Powiązane problemy