2012-08-16 16 views
5

<Imovel> etiketimin Child Nodes numaralı kitabını okuman gerekiyor, sorun şu ki, XML dosyasımda 1 (bir) <Imovel> etiketi var ve her bir <Imovel> etiketi arasındaki fark var ID adı verilen bir özelliktir. Her belirli düğümün tüm XML alt düğümlerini oku

Bu

Her <Imovel> etiketinin tüm etiketleri okumak gerek bir örnek

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

, ve benim <Imovel> etiketinde yapmak her doğrulama sonunda başka bir doğrulama yapmanız gerekir.

Yani, ben, benim örnek çok iyi LINQ hakkında anlıyorum ama uymayan 2 (iki) foreach veya for ve foreach yapmanız gereken düşünmek

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

Ama çok çalışmıyor iyi, foreach ifademde benim <Picture> etiketimden dolayı, üst etiketinde bir ID özelliği yok.

Birisi bu yöntemi yapmamda bana yardımcı olabilir mi?

+0

'Ama bu ile ne demek açıklayabilir benim foreach statement.' içinde, çok iyi çalışmaz ekleyin? –

+0

Evet, bu benim 'etiketimin ebeveyni bir ID özelliğine sahip değil. –

cevap

7

İki foreach ifadesi ile bunu mümkün olmalıdır:

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

bu deneyin. System.Xml.XPath, XElement'e xpath seçicilerini ekleyecektir. Bulma öğeleri xpath ile çok daha hızlı ve daha basittir.

Dosyayı yüklemek için XmlReader & XDocument gerekmez.

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; 
İlgili konular