使用命名空间获取 LINQ to XML 中标记的内容

发布于 2024-12-22 05:29:16 字数 1486 浏览 0 评论 0原文

我需要使用 LINQ 获取以下 XML 的 POIssuedDate 标记的内容。下面是我写的代码,我认为应该可以工作。

编辑:刚刚尝试使用 XNamespace bat = @"x-commerceone:document:btsox:Batch.sox$1.0";和 XNamespace bat = @"urn:x-commerceone:document:btsox:Batch.sox$1.0";两者都不起作用。

该代码抛出一个异常“序列不包含元素”,如果没有任何匹配给定的我使用 First() 方法

Code

XDocument baseXML = XDocument.Load(Path.Combine(XMLFolder + @"\Provide.xml"));
XNamespace bat = @"xmlns:bat=""urn:x-commerceone:document:btsox:Batch.sox$1.0";

string date = baseXML.Descendants(bat + "Batch").Elements("PurchaseOrder").Elements("OrderHeader").Elements("POIssuedDate").First().Value;

XML

<?soxtype urn:x-commerceone:document:btsox:Batch.sox$1.0?>
<?import urn:x-commerceone:document:telcoapisox:ServiceRequestOrder.sox$1.0?>
<?import urn:x-commerceone:document:com:commerceone:CBL:CBL.sox$1.0?>
<?import urn:x-commerceone:document:btsox:DSL.sox$1.0?>
<bat:Batch BatchID="B-15-6-2001-4" NoOfEntries="3" xmlns="urn:x-commerceone:document:com:commerceone:CBL:CBL.sox$1.0" xmlns:bat="urn:x-commerceone:document:btsox:Batch.sox$1.0" xmlns:sro="urn:x-commerceone:document:telcoapisox:ServiceRequestOrder.sox$1.0" xmlns:dsl="urn:x-commerceone:document:btsox:DSL.sox$1.0">
  <PurchaseOrder>
    <OrderHeader>
      <POIssuedDate>20010615T15:12:03</POIssuedDate>
..SNIP
    </OrderHeader>
  </PurchaseOrder>
</bat:Batch>

I need to get the content of the POIssuedDate tag of the below XML using LINQ. Below is the code I wrote that I think should work.

EDIT : Just tried with XNamespace bat = @"x-commerceone:document:btsox:Batch.sox$1.0"; and XNamespace bat = @"urn:x-commerceone:document:btsox:Batch.sox$1.0"; both dont work.

The code throws an exception "Sequence contains no elements" which i expect if nothing matches given im using the First() method

Code

XDocument baseXML = XDocument.Load(Path.Combine(XMLFolder + @"\Provide.xml"));
XNamespace bat = @"xmlns:bat=""urn:x-commerceone:document:btsox:Batch.sox$1.0";

string date = baseXML.Descendants(bat + "Batch").Elements("PurchaseOrder").Elements("OrderHeader").Elements("POIssuedDate").First().Value;

XML

<?soxtype urn:x-commerceone:document:btsox:Batch.sox$1.0?>
<?import urn:x-commerceone:document:telcoapisox:ServiceRequestOrder.sox$1.0?>
<?import urn:x-commerceone:document:com:commerceone:CBL:CBL.sox$1.0?>
<?import urn:x-commerceone:document:btsox:DSL.sox$1.0?>
<bat:Batch BatchID="B-15-6-2001-4" NoOfEntries="3" xmlns="urn:x-commerceone:document:com:commerceone:CBL:CBL.sox$1.0" xmlns:bat="urn:x-commerceone:document:btsox:Batch.sox$1.0" xmlns:sro="urn:x-commerceone:document:telcoapisox:ServiceRequestOrder.sox$1.0" xmlns:dsl="urn:x-commerceone:document:btsox:DSL.sox$1.0">
  <PurchaseOrder>
    <OrderHeader>
      <POIssuedDate>20010615T15:12:03</POIssuedDate>
..SNIP
    </OrderHeader>
  </PurchaseOrder>
</bat:Batch>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

懷念過去 2024-12-29 05:29:16
XNamespace bat = @"urn:x-commerceone:document:btsox:Batch.sox$1.0";
XNamespace ns  = @"urn:x-commerceone:document:com:commerceone:CBL:CBL.sox$1.0";

string date = baseXML.Descendants(bat + "Batch").Elements(ns + "PurchaseOrder").Elements(ns + "OrderHeader").Elements(ns + "POIssuedDate").First().Value;
XNamespace bat = @"urn:x-commerceone:document:btsox:Batch.sox$1.0";
XNamespace ns  = @"urn:x-commerceone:document:com:commerceone:CBL:CBL.sox$1.0";

string date = baseXML.Descendants(bat + "Batch").Elements(ns + "PurchaseOrder").Elements(ns + "OrderHeader").Elements(ns + "POIssuedDate").First().Value;
娇俏 2024-12-29 05:29:16

命名空间不应包含“xmlns:bat”部分:

XNamespace bat = "urn:x-commerceone:document:btsox:Batch.sox$1.0";
XNamespace ns = "urn:x-commerceone:document:com:commerceone:CBL:CBL.sox$1.0";

此外,您必须为每个元素指定命名空间:

string date = baseXML.Descendants(bat + "Batch")
                     .Elements(ns + "PurchaseOrder")
                     .Elements(ns + "OrderHeader")
                     .Elements(ns + "POIssuedDate")
                     .First().Value

The namespace shouldn't include the "xmlns:bat" part:

XNamespace bat = "urn:x-commerceone:document:btsox:Batch.sox$1.0";
XNamespace ns = "urn:x-commerceone:document:com:commerceone:CBL:CBL.sox$1.0";

In addition, you have to specify the namespace for every element:

string date = baseXML.Descendants(bat + "Batch")
                     .Elements(ns + "PurchaseOrder")
                     .Elements(ns + "OrderHeader")
                     .Elements(ns + "POIssuedDate")
                     .First().Value
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文