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 ?
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.
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 .
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.
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.
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.
}
}
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With