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

Asked

Viewed 1,045 times

1

I have the xml sequinte:

<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 a product happens to appear with the incorrect tax I need to know what it is, for this I use a counter in the loop, but how can I get the data of the item?

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 take the data that is inside the <det nItem="1"> ?

Follow the code until then:

'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’d have to use the Attributes, but it didn’t work out so well.

  • Have you tried this? http://stackoverflow.com/a/594243/221800 see other similar questions beside. Other http://stackoverflow.com/a/1431775/221800 If so, I can give you an answer.

  • So I didn’t get to see it, but it still doesn’t help, because the value between double quotes will be different for each item, but it will be sequential. It turns out I have to check the ICMS first and then the product if necessary. My idea was to use the counter to point, because in reality I need the contents of <Prod>

  • Is that I don’t quite understand what you want but it’s a matter of giving a small adapted. Either you LINQ or any way to scan the XML directly.

  • So, after reading the ICMS, I have a function that checks if the ICMS is valid, if not, I need to identify in which XML product the ICMS is incorrect, but as I said the products have a sequence, which is determined by the nItem=" , ex: Product 1 = <det nItem="1">, product 2 = <det nItem="2"> If product 2 has the wrong ICMS, I need the <det nitem="2 data">

  • Is there any possibility of me pointing out which nItem I need ? For example I have 10 items, I want item 6, how can I point to nItem="6" ? That would help me already.

  • @Felipewalleg. Try to import as "a table", so you can use query and comparison command, looping and etc.

Show 1 more comment

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

Note: I don’t think this is the best flow for what you need. This is just a direct answer to what you asked - reconsider iterating in the same way as Xml is structured: Read NF Header, then items, then item details, etc - always keeping the memory (state) of the hierarchy

Browser other questions tagged

You are not signed in. Login or sign up in order to post.