I am making an application to extract data from an xml and through them classify the documents in different folders. I use the XmlNodeList and the XmlElement to get the attribute of the elements but I do it with two for loops to achieve that, I would like to know if it is possible to go through the xml structure so that within a single loop I can get the "date" and "rfc ". I am making the application in VB.NET
XML structure
<?xml version="1.0" encoding="utf-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" fecha="2017-09-29T10:52:53">
<elemento rfc="XXXXXXXXXXXX" nombre="Nombre">
</elemento>
</root>
My code so far
Dim doc As XmlDocument
Dim root As XmlNodeList
Dim elemento As XmlNodeList
Dim fecha As XmlElement
Dim rfc As XmlElement
doc = New XmlDocument()
doc.Load("C:\Users\Usuario\Desktop\file.xml")
root = doc.GetElementsByTagName("root")
elemento = doc.GetElementsByTagName("elemento")
For Each fecha In root
strFecha = fecha.GetAttribute("fecha")
Next
For Each rfc In elemento
strRFC = rfc.GetAttribute("rfc")
Next
For this, nothing better than linq to XML to the rescue!!!
Let's see, I made a small example program, with your example and demonstrating how to obtain the values that you want.
What we do is basically take advantage of the fact that LINQ already knows how to read an xml, and based on that, we obtain the parts that we want. For Linq, every node is an element (XElement). so that way, we can get the root node, and from there go looking for the other things we need.
Since it is not clear whether or not an element can appear more than once, another XML is included with a for each, which in this case would allow that value to be obtained more than once.