Optimize path of XML nodes in VB.NET

0

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 extract the attribute from the elements but I do it with two for cycles to achieve that, I would like to know if it is possible to go through the xml structure so that within a single cycle I can obtain the "date" and "rfc" " The application I'm doing 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
    
asked by Silvestre Silva 08.03.2018 в 19:04
source

1 answer

2

For this, nothing better than linq to XML to the rescue !!!

Let's see, I made a small sample program, with your example and demonstrating how to obtain the values that you want.

Dim nodoraiz As XElement
Dim Xml As String = "<?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 > "
Dim doc As XDocument = New XDocument()
doc = XDocument.Parse(Xml)
nodoraiz = doc.Element("root")
Dim fecha As String = nodoraiz.Attribute("fecha").Value

Console.WriteLine(fecha)

Dim rfc As String = nodoraiz.Element("elemento").Attribute("rfc").Value

Console.WriteLine(rfc)

Xml = "<?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 = ""XXXXXXXXXXX1"" nombre = ""Nombre"" ></elemento ><elemento rfc = ""XXXXXXXXXXX2"" nombre = ""Nombre"" ></elemento ></root > "
doc = XDocument.Parse(Xml)
nodoraiz = doc.Element("root")
fecha = nodoraiz.Attribute("fecha").Value

Console.WriteLine(fecha)

For Each v In nodoraiz.Elements("elemento")
    rfc = v.Attribute("rfc").Value
    Console.WriteLine(rfc)

Next
Console.ReadKey()

What we do is basically, take advantage of LINQ already know how to read an xml, and based on that go getting the parts we want. For Linq, every node is an element (XElement). then in that way, we can get the root node, and then go looking for the other things we need.

As it is not clear if the element may or may not appear more than once, another XML is included with a for each, which in that case would allow that value to be obtained more than once.

    
answered by 09.03.2018 / 17:09
source