我正在编写一个C#类,它允许我从OData feed构造实体。 不要问为什么,我现在只需要它:)
XML的片段如下所示:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<entry xml:base="http://demo.tenforce.acc/Api.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<id>http://demo.tenforce.acc/Api.svc/Items(387)</id>
<title type="text"></title>
<updated>2011-09-22T07:35:54Z</updated>
<author>
<name />
</author>
<link rel="edit" title="Item" href="Items(387)" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Children" type="application/atom+xml;type=feed" title="Children" href="Items(387)/Children" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Parent" type="application/atom+xml;type=entry" title="Parent" href="Items(387)/Parent" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Attachments" type="application/atom+xml;type=feed" title="Attachments" href="Items(387)/Attachments" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Predecessors" type="application/atom+xml;type=feed" title="Predecessors" href="Items(387)/Predecessors" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Successors" type="application/atom+xml;type=feed" title="Successors" href="Items(387)/Successors" />
<category term="TenForce.Execution.Api2.Objects.Item" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:Id m:type="Edm.Int32">387</d:Id>
</m:properties>
</content>
</entry>
我创建了一个代码片段,将整个xml字符串加载到XmlDocument中,并生成XmlNamespaceManager以访问各种命名空间。
我正在尝试从XML中选择<category>
元素,但我似乎无法正确获取Xpath表达式。我尝试了以下内容:
但似乎没有人选择有问题的节点。
答案 0 :(得分:0)
哦,没关系,我找到了解决方案。 我不得不为原子元素添加命名空间....
var manager = new XmlNamespaceManager(_mDocument.NameTable);
manager.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");
manager.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
manager.AddNamespace("atom", "http://www.w3.org/2005/Atom");
这允许我使用// atom:category
选择<category>
元素