3

我有一个带有多个名称空间的 XML 文件。但我无法从任何节点获取值/文本。

<?xml version="1.0" encoding="UTF-8"?>
<!--XML file created by Microsoft Dynamics C5-->
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema"
         xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
         xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
         xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2"
         xmlns:ccts="urn:un:unece:uncefact:documentation:2"
         xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
         xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2">
  <cbc:UBLVersionID schemeAgencyID="320" schemeAgencyName="urn:oioubl:id:profileid-1.1">2.0</cbc:UBLVersionID>
  <cbc:CustomizationID>OIOUBL-2.02</cbc:CustomizationID>
  <cbc:ProfileID schemeID="urn:oioubl:id:profileid-1.2" schemeAgencyID="320">Procurement-BilSim-1.0</cbc:ProfileID>
  <cbc:ID>88481</cbc:ID>
  <cbc:IssueDate>2012-05-21</cbc:IssueDate>
  <cbc:InvoiceTypeCode listID="urn:oioubl:codelist:invoicetypecode-1.1" listAgencyID="320">380</cbc:InvoiceTypeCode>
  <cbc:DocumentCurrencyCode>DKK</cbc:DocumentCurrencyCode>
  <cbc:AccountingCost></cbc:AccountingCost>
  <cac:OrderReference>
    <cbc:ID>ZZ</cbc:ID>
    <cbc:SalesOrderID>36433</cbc:SalesOrderID>
    <cbc:IssueDate>2012-05-21</cbc:IssueDate>
  </cac:OrderReference>
</Invoice>

我正在尝试读取一些节点,但我不能。这是一个例子。

XmlDocument doc = new XmlDocument();
doc.Load(@"C:\temp\88481.xml");

XmlNamespaceManager manager = new XmlNamespaceManager(doc.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");
manager.AddNamespace("", "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2");
XmlNodeList list = doc.SelectNodes("//Invoice/cbc:ID", manager);

但是节点列表没有元素?

4

2 回答 2

5

鉴于评论,我将假设您可以改用 LINQ to XML:

XDocument doc = XDocument.Load("...");
XNamespace cbcNs = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2";
XNamespace cacNs = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2";
// etc
XNamespace topNs = "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2";

var ids = doc.Elements(topNs + "Invoice")
             .Elements(cbc + "ID");

或者因为顶部只是根元素,最后的语句可以是:

var ids = doc.Root.Elements(cbc + "ID");
于 2012-10-15T13:50:37.903 回答
2

你第一次真的很亲近。

就像 Jon Skeet 提到的,“Invoice”是根元素,所以你不需要声明一个新的命名空间。

XmlDocument doc = new XmlDocument();
doc.Load(@"C:\temp\88481.xml");

XmlNamespaceManager manager = new XmlNamespaceManager(doc.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");
//manager.AddNamespace("", "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2");

XmlNodeList list = doc.SelectNodes("//cbc:ID", manager);
于 2015-08-26T10:04:09.720 回答