Lesen Sie alle untergeordneten XML-Knoten eines jeden bestimmten Knoten

Muss ich alle Lesen die Child Nodes meiner <Imovel> tag, das problem ist, dass ich mehr als 1 (ein) <Imovel> - tag in der XML-Datei, und der Unterschied zwischen den einzelnen <Imovel> - tag ein Attribut namens ID.

Dies ist ein Beispiel

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

Ich lese alle tags von jedem <Imovel> tag, und am Ende jeder Bestätigung, dass ich in meinem <Imovel> tag habe ich eine weitere Validierung.

So, ich denke ich brauche 2 (zwei) foreach oder eine for und ein foreach, ich verstehe nicht sehr gut über LINQ aber Folgen Sie meinem Beispiel

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

Aber nicht sehr gut funktioniert, in meinem foreach Aussage, weil meine <Picture> - tag, Eltern-tag nicht über ein ID-Attribut.

Jemand kann mir helfen, das zu tun diese Methode ?

But doesn't work very well, in my foreach statement. können Sie erklären, was Sie bedeuten?
Ja, das ist weil die Eltern meines <Picture> tag nicht hat ein ID-Attribut

InformationsquelleAutor Lucas_Santos | 2012-08-16

Schreibe einen Kommentar