Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read all XML child nodes of each specific node

Tags:

c#

linq

I need to read all the Child Nodes of my <Imovel> tag, the problem is that I has more than 1 (one) <Imovel> tag in my XML file, and the difference between each <Imovel> tag is an attribute called ID.

This is an example

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

I need read all tags of each <Imovel> tag, and in the end of each validation that I do in my <Imovel> tag I need to do another validation.

So, I think I need to do 2 (two) foreach or a for and a foreach, I don't understand very well about LINQ but follow my sample

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

But doesn't work very well, in my foreach statement because my <Picture> tag, her parent tag don't has an ID attribute.

Somebody can help me to do this method ?

like image 707
Lucas_Santos Avatar asked Aug 16 '12 19:08

Lucas_Santos


People also ask

How do I select a specific node in XML?

To find nodes in an XML file you can use XPath expressions. Method XmlNode. SelectNodes returns a list of nodes selected by the XPath string. Method XmlNode.

What is the difference between XDocument and XmlDocument?

XDocument is from the LINQ to XML API, and XmlDocument is the standard DOM-style API for XML. If you know DOM well, and don't want to learn LINQ to XML, go with XmlDocument .

What is ChildNodes in XML?

The childNodes property is a read-only property containing a node list of all children for those elements that can have them. It returns a NodeList for the following valid node types: NODE_ATTRIBUTE.

What is XmlNodeList?

XmlNodeList supports iteration and indexed access. XmlNodeList is returned by the following properties and methods. XmlNode. ChildNodes - Returns an XmlNodeList containing all the children of the node.


2 Answers

You should be able to do this with two foreach statements:

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.
  }
}
like image 130
Abe Miessler Avatar answered Oct 28 '22 01:10

Abe Miessler


try this. System.Xml.XPath will add xpath selectors to XElement. Finding elements is much quicker and simpler with xpath.

You don't need XmlReader & XDocument to load the 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);
}

please include

System.Xml.XPath;
like image 25
maruthu chandrasekaran Avatar answered Oct 28 '22 01:10

maruthu chandrasekaran