How to get data from a specific field in an XML using LINQ VB.net/C#

1

I have the following xml:

<det nItem="1">
    <prod>
        <cProd>01554</cProd>
        <xProd>La Aslan Velour 0001 </xProd>
        <NCM>55111000</NCM>
        <CFOP>5102</CFOP>
        <uCom>UN</uCom>
        <qCom>1.0000</qCom>
        <vUnCom>6.90</vUnCom>
        <indRegra>A</indRegra>
    </prod>
    <imposto>
        <ICMS>
            <ICMSSN102>
                <Orig>0</Orig>
                <CSOSN>102</CSOSN>
            </ICMSSN102>
        </ICMS>
    </imposto>
 </det>

I have a loop that validates my products, it checks the taxes and return an error if the tax is incorrect. If it happens that a product appears with the incorrect tax I need to know what it is, for that I use a counter in the loop, but how can I get the item data?

The items start with a <det nItem="1"> , what will change is the number that will indicate the item, but how can I point my counter to get the data that is within <det nItem="1"> ?

Follow the code so far:

'Loop de todos os ICMS dos produtos
For Each ReadXML In document.Descendants("ICMS")

    'Contador de produtos
     Contador_Prod += 1

       dim Prod = ReadXML.Descendants.Attributes(Contador_Prod)

 Next

I thought I would have to use Attributes , but it did not work very well.

    
asked by anonymous 17.07.2015 / 17:25

1 answer

1
For Each tagICMS As XmlNode In document.GetElementsByTagName("ICMS"))
  Dim tagImposto As XmlNode = tagICMS.ParentNode 
  Dim tagDet As XmlNode = tagImposto.ParentNode
  Dim attribNItem As XmlAttribute = tagDet.Attributes("nItem")
  Dim nrDetalheAtual = CInt(attribNItem.Value)
Next

Obs: I do not think this is the best flow for what you need. This is just a straightforward answer to what you asked yourself - reconsider iterating in the same way as Xml is structured: Read NF Header, then items, then item details, etc. - always keeping the hierarchy memory

    
26.03.2017 / 23:12