开发者

How to Parse XML having multiple Default Namespace?

开发者 https://www.devze.com 2022-12-23 22:24 出处:网络
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/\"

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 us开发者_JS百科ing 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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消