提问人:Fernando Diaz 提问时间:8/28/2023 最后编辑:Fernando Diaz 更新时间:8/28/2023 访问量:56
读取 XML 发票节点的内容
Read content of an XML invoice node
问:
我正在尝试读取包含电子发票的 xml 文件的内容。为此,我使用命名空间,但我无法提取在 cac:AdditionalItemProperty 元素中找到的数据,特别是 cbc:Name02</cbc:Name>cbc:Value2023171843</cbc:Value> 我所做的代码如下,也是 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>
'代码 VbNet
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)
答:
0赞
SSS
8/28/2023
#1
CDATA 节内有第二个 XML 文件。您需要单独解析它。类似于下面的代码。我无法测试它,因为您包含的 XML 似乎不完整:
Dim m_xmld = New XmlDocument
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("ext", "urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2")
m_xmld.Load(fileName)
Dim nodeDescription As XmlNode = m_xmld.SelectSingleNode("//cbc:Description", manager)
Dim childDoc = New XmlDocument
childDoc.LoadXml(nodeDescription.FirstChild.InnerText.Trim) '<< parse the CDATA section
Dim additional = childDoc.SelectSingleNode("//cac:AdditionalItemProperty[0]")
MessageBox.Show(additional.InnerText, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
评论
0赞
Fernando Diaz
8/28/2023
执行这些更改时,它会在 childDoc.Load(nodeDescription.FirstChild.InnerText) 行中显示一个错误。附加信息:路径中的字符无效。
0赞
SSS
8/28/2023
抱歉,应该是.我已经更新了答案childDoc.LoadXml(nodeDescription.FirstChild.InnerText.Trim)
0赞
Fernando Diaz
8/29/2023
谢谢,XML 现已完成。使用新行,它不再在其中抛出错误,但它会在此 MessageBox.Show(additional.InnerText.ToString、Me.Text、MessageBoxButtons.OK、MessageBoxIcon.Exclamation) 它给出的错误消息是:“附加信息:对象引用未设置为对象的实例。在调试中,附加变量的值为 Nothing
0赞
SSS
8/29/2023
恐怕您将不得不自己完成其余的调试。您提供的示例 XML 格式不正确。您需要确保每个开始标记在示例中都有一个匹配的结束标记
0赞
Fernando Diaz
8/29/2023
当然,我编辑了它并记录了机密信息问题的随机信息
下一个:如何访问后代节点的值
评论