How to Parse an XML (using XELement) having multiple Namespace?
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/"
开发者_如何学JAVA 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
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.
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>
精彩评论