使用命名空间获取 LINQ to XML 中标记的内容
我需要使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
命名空间不应包含“xmlns:bat”部分:
此外,您必须为每个元素指定命名空间:
The namespace shouldn't include the "xmlns:bat" part:
In addition, you have to specify the namespace for every element: