How to parse XML (using XELement) with multiple namespaces?

I get the following Xresponse request after parsing an 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> 

In accordance with the previous thread. When a document has a default namespace, you should parse it as if it were a named namespace. For instance.

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 namespaces. I do not get any results after using the following query, I get a response without getting 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 idea what happens when I'm new to this. Please, help

+3
source share
2 answers

, 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(),
};

XNames .

+3

, . urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/.

http://purl.org/dc/elements/1.1/ dc: xml. , , .

- 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>
+3

Source: https://habr.com/ru/post/1738009/


All Articles