2012-08-16 29 views
5

Tôi cần đọc tất cả các Child Nodes của thẻ <Imovel>, vấn đề là tôi có nhiều hơn 1 (một) <Imovel> thẻ trong tệp XML của mình và sự khác biệt giữa mỗi thẻ <Imovel> là một thuộc tính được gọi là ID.Đọc tất cả các nút con XML của từng nút cụ thể

Đây là một ví dụ

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

Tôi cần đọc tất cả các thẻ của mỗi thẻ <Imovel>, và ở cuối mỗi xác nhận rằng tôi làm trong thẻ <Imovel> của tôi, tôi cần phải làm xác nhận khác.

Vì vậy, tôi nghĩ rằng tôi cần phải làm 2 (hai) foreach hoặc một forforeach, tôi không hiểu rất rõ về LINQ nhưng làm theo mẫu của tôi

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

Nhưng không hoạt động rất tốt, trong tuyên bố của tôi foreach vì thẻ <Picture> của tôi, thẻ gốc của cô ấy không có thuộc tính ID.

Ai đó có thể giúp tôi thực hiện phương pháp này?

+0

'Nhưng không hoạt động rất tốt, trong statement.' foreach của tôi bạn có thể giải thích những gì bạn có nghĩa là bằng cách này? –

+0

Có, điều này là do cha mẹ của thẻ '' không có thuộc tính ID –

Trả lời

7

Bạn sẽ có thể làm điều này với hai câu lệnh 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

thử này. System.Xml.XPath sẽ thêm bộ chọn xpath vào XElement. Tìm kiếm các yếu tố nhanh hơn và đơn giản hơn với xpath.

Bạn không cần XmlReader & XDocument để tải tệp.

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

xin vui lòng bao gồm

System.Xml.XPath; 
Các vấn đề liên quan