如何解析具有多个命名空间的 XML(使用 XELement)?

发布于 2024-08-26 05:58:21 字数 1115 浏览 2 评论 0原文

解析 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 技术交流群。

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

发布评论

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

评论(2

ぇ气 2024-09-02 05:58:21

这是因为您的 item 元素位于您的“ns”命名空间中。使用:-

XNamespace dc = "http://purl.org/dc/elements/1.1/";
XName didl = ns + "DIDL-lite";
XName item = ns + "item";
XName title = dc + "title";

var vAudioData = from xAudioinfo in xResponse.Descendants(didl).Elements(item)

select new RMSMedia
{
     strAudioTitle = ((string)xAudioinfo.Element(title)).Trim(),
};

在这些情况下,我倾向于为自己创建一个私有类来保存简化查询代码所需的 XName 集。

This is because your item element is in your "ns" namespace. Use:-

XNamespace dc = "http://purl.org/dc/elements/1.1/";
XName didl = ns + "DIDL-lite";
XName item = ns + "item";
XName title = dc + "title";

var vAudioData = from xAudioinfo in xResponse.Descendants(didl).Elements(item)

select new RMSMedia
{
     strAudioTitle = ((string)xAudioinfo.Element(title)).Trim(),
};

In these cases I tend to create myself a private class to hold the set of XNames I need to simplify the query code.

雨落星ぅ辰 2024-09-02 05:58:21

您没有得到任何结果,因为您使用了错误的命名空间。所有没有前缀的元素都位于命名空间 urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/ 中。

在 xml 文档中,命名空间 http://purl.org/dc/elements/1.1/ 中的项目以 dc: 为前缀。该片段不显示任何项目,因此很难判断您正在寻找哪些元素。

例如 - 给定以下 xml:

<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> 
<dc:title>Purl Title</dc:title>
</item> 
</DIDL-Lite>

并且假设您想要检索两个标题,以下代码应该产生您正在寻找的结果:

XNamespace dc= "http://purl.org/dc/elements/1.1/";
XNamespace ns = "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/";

var result = xAudioinfo.Descendants(ns + "title"); // <title></title>
var result2 = xAudioinfo.Descendants(dc + "title"); // <dc:title></dc:title>

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 with dc: 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:

<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> 
<dc:title>Purl Title</dc:title>
</item> 
</DIDL-Lite>

And also given the assumption that you want to retrieve both titles the following code should yiedl the results you are looking for:

XNamespace dc= "http://purl.org/dc/elements/1.1/";
XNamespace ns = "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/";

var result = xAudioinfo.Descendants(ns + "title"); // <title></title>
var result2 = xAudioinfo.Descendants(dc + "title"); // <dc:title></dc:title>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文