views:

136

answers:

2

Here is my XML Response:

 <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/"
<item id="1182" parentID="40" restricted="1">
<title>Hot Issue</title>
</item>
</DIDL-Lite>

When I am trying to parse it using xELemnt and try assigning to a var like below:

   var vnyData = from xmyResponse in xResponse.Descendants("DIDL-Lite").Elements("item")
select new myClass
                                     {strTitle = ((string)xmyResponse .Element("title")).Trim()};

This is not yeilding any results.

Thanks, Subhendu

+1  A: 

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");

Whatever you name the ns variable is unimportant. The key is that anywhere you are passing an element name (XName to be precise) you need to include the namespace + name. You'll note that string is convertible to XNamespace, but you could also use its constructor.

Josh Einstein
Thanks Mr. Einstein. In my document there are multiple Namespace.So in that case do I have to write:XNamespace ns = "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/";XNamespace ns1 = "http://purl.org/dc/elements/1.1/";XNamespace ns2 = "urn:schemas-upnp-org:metadata-1-0/upnp/";var xDIDL = xResponse.Element(ns + ns1 + ns2 + "DIDL-Lite");
Subhen
No, in your XML document any element that does not have a prefix is in the "default" namespace (that is, the one that says xmlns="..." as opposed to xmlns:prefix="...") so you only need to do ns+"ElementName". However, if you were referencing an element in another namespace you'd have to do ns1+"ElementName" or ns2+"ElementName" but never more than one namespace for a particular element name.
Josh Einstein
A: 

You are using your xml schema which should be present in names of the elements you are trying to access. Check out XNamespace class.

Andrew Bezzub