如何解析具有多个命名空间的 XML(使用 XELement)?
解析 XML 文档后,我得到以下 Xresponse:
<DIDL-Lite
xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/"
xmlns:dlna="urn:schemas-dlna-org:metadata-1-0/">
<item id="1182" parentID="40" restricted="1">
<title>Hot Issue</title>
</item>
根据之前的线程,当文档中存在默认命名空间时,您必须将其解析为命名命名空间。例如。
XNamespace ns = "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/";
var xDIDL = xResponse.Element(ns + "DIDL-Lite");
但就我而言,我有四个不同的名称空间。使用以下查询后我没有得到任何结果,我得到了响应,但没有产生任何结果:
XNamespace dc = "http://purl.org/dc/elements/1.1/";
var vAudioData = from xAudioinfo in xResponse.Descendants(ns + "DIDL-lite").Elements("item")
select new RMSMedia
{
strAudioTitle = ((string)xAudioinfo.Element(dc + "title")).Trim(),
};
我不知道发生了什么,因为我是新手。请帮忙
I get the followinng Xresponse after parsing the XML document:
<DIDL-Lite
xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/"
xmlns:dlna="urn:schemas-dlna-org:metadata-1-0/">
<item id="1182" parentID="40" restricted="1">
<title>Hot Issue</title>
</item>
As per the earlier thread, When there is a default namespace in the document, you must parse it as if it were a named namespace. For example.
XNamespace ns = "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/";
var xDIDL = xResponse.Element(ns + "DIDL-Lite");
But in my case I have four different name space. I am not getting any results after using the following query , I am getting the response , Not Yeilding any results:
XNamespace dc = "http://purl.org/dc/elements/1.1/";
var vAudioData = from xAudioinfo in xResponse.Descendants(ns + "DIDL-lite").Elements("item")
select new RMSMedia
{
strAudioTitle = ((string)xAudioinfo.Element(dc + "title")).Trim(),
};
I have no clue whats going on as am new to it. Please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为您的
item
元素位于您的“ns”命名空间中。使用:-在这些情况下,我倾向于为自己创建一个私有类来保存简化查询代码所需的 XName 集。
This is because your
item
element is in your "ns" namespace. Use:-In these cases I tend to create myself a private class to hold the set of XNames I need to simplify the query code.
您没有得到任何结果,因为您使用了错误的命名空间。所有没有前缀的元素都位于命名空间
urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/
中。在 xml 文档中,命名空间
http://purl.org/dc/elements/1.1/
中的项目以dc:
为前缀。该片段不显示任何项目,因此很难判断您正在寻找哪些元素。例如 - 给定以下 xml:
并且假设您想要检索两个标题,以下代码应该产生您正在寻找的结果:
You are not getting any results because you are using the wrong namespace. All elements without prefix are in the namespace
urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/
.Items in the namespace
http://purl.org/dc/elements/1.1/
are prefixed withdc:
in the xml document. The fragment does not show any items so it is hart to tell what elements you are looking for.For example - given the following xml:
And also given the assumption that you want to retrieve both titles the following code should yiedl the results you are looking for: