Read tag fields in XML VB.Net
Good morning gentlemen,
I need to read a field that is inside a tag in xml but I am having difficulties, I would need to do this to set up a counter.
My Project is to read the XML of a sale and assemble a tax coupon from it, but so far I have only managed to pull an item from the sale. Could you help me ?
XML structure, in case I would need to read the field "nItem" that is in the tag "det", because so maybe I could pull all items:
<det nItem="1">
<prod>
<cProd>7898577370182</cProd>
<xProd>J.WATANABE/COUVE MANTE</xProd>
<vItem>0.99</vItem>
</prod>
</det>
<det nItem="2">
<prod>
<cProd>66396</cProd>
<xProd>GRANEL/ALHO KG</xProd>
<vItem>1.62</vItem>
</prod>
</det>
Structure VB.NET
Dim produto As New BindingSource
produto.DataSource = ds
produto.DataMember = "prod"
Dim descricao As String = produto.Current("xProd").ToString()
Dim quantidade As String = produto.Current("cProd").ToString()
Dim valorprod As String = produto.Current("vProd").ToString()
2
Author: João Martins, 2019-03-13
1 answers
Here is an example that can help you in the solution:
Dim strXml As String =
"<xml>
<det nItem=""1"">
<prod>
<cProd>7898577370182</cProd>
<xProd>J.WATANABE/COUVE MANTE</xProd>
<vItem>0,99</vItem>
</prod>
</det>
<det nItem=""2"">
<prod>
<cProd>66396</cProd>
<xProd>GRANEL/ALHO KG</xProd>
<vItem>1,62</vItem>
</prod>
</det>
</xml>"
Dim xmlDoc As XmlDocument = New XmlDocument()
xmlDoc.LoadXml(strXml)
Dim xmlNodeList As XmlNodeList = xmlDoc.GetElementsByTagName("det")
After you have the object xmlNodeList
filled, you can count the number of occurrences (if that's what you want):
Dim nodeCount As Integer = xmlNodeList.Count
Can still go through each node and do additional processing (get value from properties, child nodes, etc):
For Each node As XmlNode In xmlNodeList
Dim value = node.Attributes.GetNamedItem("nItem").Value
Dim cProd As String = Convert.ToString(node("prod")("cProd").InnerText)
Dim xProd As String = Convert.ToString(node("prod")("xProd").InnerText)
Dim vItem As Double = Convert.ToDouble(node("prod")("vItem").InnerText)
Next
1
Author: João Martins, 2019-03-19 15:25:51