Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to read the contents of an XML file that contains an electronic invoice. For this, I use namespace but I have not been able to extract the data found in the <cac:additionalitemproperty> element.
The code that I do is the following and additionally part of the XML:

XML
<AttachedDocument xmlns="urn:oasis:names:specification:ubl:schema:xsd:AttachedDocument-2" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ccts="urn:un:unece:uncefact:data:specification:CoreComponentTypeSchemaModule:2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" xmlns:xades141="http://uri.etsi.org/01903/v1.4.1#">
<ext:UBLExtensions>
...
</ext:UBLExtensions>
<cbc:UBLVersionID>UBL 2.1</cbc:UBLVersionID>
<cbc:CustomizationID>Doctos</cbc:CustomizationID>
<cbc:ID>DT41647</cbc:ID>
<cbc:IssueDate>2023-03-17</cbc:IssueDate>
<cbc:ParentDocumentID>DT41647</cbc:ParentDocumentID>
<cac:SenderParty>
...
</cac:SenderParty>
<cac:ReceiverParty>
...
</cac:ReceiverParty>
<cac:Attachment>
<cac:ExternalReference>
<cbc:MimeCode>text/xml</cbc:MimeCode>
<cbc:EncodingCode>UTF-8</cbc:EncodingCode>
<cbc:Description>
<![CDATA[ <?xml version="1.0" encoding="UTF-8" standalone="no"?><Invoice xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sts="xxxx" xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" xmlns:xades141="http://uri.etsi.org/01903/v1.4.1#" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:oth="http://example.org/oth" xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 http://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/maindoc/UBL-Invoice-2.1.xsd" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"><ext:UBLExtensions>
<ext:UBLExtension><ext:ExtensionContent>
<sts:xxxExtensions><sts:InvoiceControl><sts:InvoiceAuthorization>99999999</sts:InvoiceAuthorization><sts:AuthorizationPeriod><cbc:StartDate>2023-02-23</cbc:StartDate><cbc:EndDate>2024-02-23</cbc:EndDate></sts:AuthorizationPeriod>
<cac:AdditionalItemProperty><cbc:Name>
01</cbc:Name><cbc:Value>105584152</cbc:Value>
</cac:AdditionalItemProperty><cac:AdditionalItemProperty>
<cbc:Name>02</cbc:Name><cbc:Value>2023171843
</cbc:Value></cac:AdditionalItemProperty>
<cac:AdditionalItemProperty><cbc:Name>03
</cbc:Name><cbc:Value>184000</cbc:Value>
<cbc:ValueQuantity unitCode="KG">1280</cbc:ValueQuantity>
</cac:AdditionalItemProperty></cac:Item>
<cac:Price><cbc:PriceAmount currencyID="PP">184000.00</cbc:PriceAmount>
<cbc:BaseQuantity unitCode="94">1.00</cbc:BaseQuantity>
</cac:Price></cac:InvoiceLine>
</Invoice> ]]>
</cbc:Description>
</cac:ExternalReference>
</cac:Attachment>
<cac:ParentDocumentLineReference>
...
</cac:ParentDocumentLineReference>
</AttachedDocument>


What I have tried:

VB
m_xmld = New XmlDocument
   m_xmld.Load(fileName)
   Dim manager As XmlNamespaceManager = New XmlNamespaceManager(m_xmld.NameTable)
   manager.AddNamespace("cbc", "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")
   manager.AddNamespace("cac", "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")
   'manager.AddNamespace("qdt", "urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2")
   'manager.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema")
   'manager.AddNamespace("udt", "urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2")
   'manager.AddNamespace("ccts", "urn:un:unece:uncefact:documentation:2")
   manager.AddNamespace("ext", "urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2")
   Dim list As XmlNodeList = m_xmld.SelectNodes("//cac:Attachment//cac:ExternalReference//cbc:Description", manager) 'da resultado 2
   'Dim nodeComprobante As XmlNode = m_xmld.SelectSingleNode("//cac:AdditionalItemProperty", manager)
   Dim nodeComprobante As XmlNode
   For Each Book As XmlNode In list
       nodeComprobante = m_xmld.SelectSingleNode("//cac:AdditionalItemProperty", manager)
       'TotalXML += Book.SelectSingleNode("//cac:AdditionalItemProperty", manager).InnerXml 'error
       'Dim mCodigo As Object = New Book.Attributes.GetNamedItem("Name").Value 'error
   Next
   MessageBox.Show("Tota: " & TotalXML, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Posted
Updated 28-Aug-23 9:11am
v2

For some bizarre reason, your XML document contains another XML document embedded as a string within a node. Your <cac:AdditionalItemProperty> elements belong to this embedded document; you won't be able to query them from the parent document.

Unfortunately, the XML you've shown is invalid. It's not clear whether that's a result of you editing it for the question, or whether it's really malformed. But as it stands, the structure is:
XML
<Invoice ...>
    <ext:UBLExtensions>
        <ext:UBLExtension>
            <ext:ExtensionContent>
                <sts:xxxExtensions>
                    <sts:InvoiceControl>
                        <sts:InvoiceAuthorization>
                        </sts:InvoiceAuthorization>
                        <sts:AuthorizationPeriod>
                            <cbc:StartDate>
                            </cbc:StartDate>
                            <cbc:EndDate>
                            </cbc:EndDate>
                        </sts:AuthorizationPeriod>
        
        ... CONTENT MISSING ...
        
            <cac:AdditionalItemProperty>
                <cbc:Name>
                </cbc:Name>
                <cbc:Value>
                </cbc:Value>
            </cac:AdditionalItemProperty>
            <cac:AdditionalItemProperty>
                <cbc:Name>
                </cbc:Name>
                <cbc:Value>
                </cbc:Value>
            </cac:AdditionalItemProperty>
            <cac:AdditionalItemProperty>
                <cbc:Name>
                </cbc:Name>
                <cbc:Value>
                </cbc:Value>
                <cbc:ValueQuantity>
                </cbc:ValueQuantity>
            </cac:AdditionalItemProperty>
        </cac:Item>
        <cac:Price>
            <cbc:PriceAmount>
            </cbc:PriceAmount>
            <cbc:BaseQuantity>
            </cbc:BaseQuantity>
        </cac:Price>
    </cac:InvoiceLine>
</Invoice>

Assuming that's just you editing the document for the question, and the real XML is valid, you'll need to parse the embedded XML document to a new document before you can query it:
VB.NET
For Each Book As XmlNode In list
    Dim bookXml As New XmlDocument()
    bookXml.LoadXml(Book.InnerText.Trim())
    Dim propertyNode As XmlNode = bookXml.SelectSingleNode("//cac:AdditionalItemProperty", manager)
    ...
Next
 
Share this answer
 
Comments
Member 12760369 30-Aug-23 1:08am    
Thank you. Indeed in order not to publish sensitive information, I edited the document. The VBNET code manages to get into the loop you send me, but I haven't been able to get the values ​​of the propertyNode element. The XML with the corresponding tags is as follows

<cbc:Description>
<![CDATA[ <?xml version="1.0" encoding="utf-8" standalone="no"?><ApplicationResponse xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:sts="dian:gov:co:facturaelectronica:Structures-2-1" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns="urn:oasis:names:specification:ubl:schema:xsd:ApplicationResponse-2"> 
<ext:UBLExtensions> <ext:UBLExtension> <ext:ExtensionContent> <sts:DDDExtensions> <sts:InvoiceSource> <cbc:IdentificationCode listAgencyID="6" listAgencyName="United Nations Economic Commission for Europe" listSchemeURI="urn:oasis:names:specification:ubl:codelist:gc:CountryIdentificationCode-2.1">CO</cbc:IdentificationCode> </sts:InvoiceSource> <sts:SoftwareProvider> <sts:ProviderID schemeID="2" schemeName="13" schemeAgencyID="19" schemeAgencyName="Enterprise">999999999</sts:ProviderID> <sts:SoftwareID schemeAgencyID="19" schemeAgencyName="Enterprise">...</sts:SoftwareID> </sts:SoftwareProvider> 
<sts:SoftwareSecurityCode schemeAgencyID="19" schemeAgencyName="Enterprise">...</sts:SoftwareSecurityCode> <sts:AuthorizationProvider> <sts:AuthorizationProviderID schemeID="2" schemeName="13" schemeAgencyID="19" schemeAgencyName="Enterprise">999999999</sts:AuthorizationProviderID> </sts:AuthorizationProvider> </sts:DDDExtensions> </ext:ExtensionContent> </ext:UBLExtension> <ext:UBLExtension> <ext:ExtensionContent><ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="SignatureXXX"><ds:SignedInfo><ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" /><ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" /><ds:Reference Id="ReferenceXXX" URI=""><ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" /></ds:Transforms><ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
<ds:DigestValue>MMMMMMMMMMMMM</ds:DigestValue></ds:Reference><ds:Reference Id="ReferenceKeyInfo" URI="#SignatureXXX-KeyInfo"><ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" /><ds:DigestValue>/UbeitEkyZn4cM2EJb7NqeJG/AKn51rEbNNaVjbVwB8=</ds:DigestValue></ds:Reference><ds:Reference Type="http://uri.etsi.org/01903#SignedProperties" URI="#xmldsig-SignatureXXX-signedprops"><ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" /><ds:DigestValue>AAAAAAAAAAAA</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue Id="SignatureValue-TTTTTTTTTTT">YUTYUTYUTYU76876uyuyu</ds:SignatureValue><ds:KeyInfo Id="SignatureXXX-KeyInfo"><ds:X509Data><ds:X509Certificate>87t7878t78t65yyutyutyutyutyutuytvyutyutuytv</ds:X509Certificate></ds:X509Data>
<ds:KeyValue><ds:RSAKeyValue><ds:Modulus>uyu87687v8v67868767866fgfftftftyfytft</ds:Modulus><ds:Exponent>AQAB</ds:Exponent></ds:RSAKeyValue></ds:KeyValue></ds:KeyInfo><ds:Object Id="ook.8888mmmuuguy55454bb"><xades:QualifyingProperties xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" Id="QualifyingProperties-35e50bb6-9e6f-4b33-9334-8abb56fb3953" Target="#SignatureXXX"><xades:SignedProperties Id="xmldsig-SignatureXXX-signedprops"><xades:SignedSignature
Richard Deeming 30-Aug-23 3:10am    
There are no cac:AdditionalItemProperty nodes in that comment; your XML has been truncated.

Rather than trying to post the XML in a comment, click the green "Improve Question" link and update the XML in your question.
VB
Dim nodeDescription As XmlNode = m_xmld.SelectSingleNode("//cbc:Description", manager)
                Dim childDoc = New XmlDocument
                childDoc.LoadXml(nodeDescription.FirstChild.InnerText.Trim) 
                Valuerem = ""
                Dim additional = childDoc.SelectSingleNode("//cbc:ID", manager)
                For Each xnd As XmlNode In additional
                    If Not xnd Is Nothing Then
                        Valuerem = xnd.Value
                     End If
                Next

                Dim additional2 = childDoc.SelectNodes("//cac:AdditionalItemProperty[02]", manager)
                For Each node As XmlNode In additional2
                    Dim oNode = node.SelectSingleNode("cbc:Value", manager)
                Next
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900