How to get the child elementsvalue , when the parent element contains different number of child Elem

Posted by Subhen on Stack Overflow See other posts from Stack Overflow or by Subhen
Published on 2010-04-01T07:31:20Z Indexed on 2010/04/01 7:33 UTC
Read the original article Hit count: 642

Filed under:
|
|
|
|

Hi, I have the following XML Structure:

 <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="1268" parentID="20" restricted="1">
    <upnp:class>object.item.audioItem.musicTrack</upnp:class>
    <dc:title>Hey</dc:title>
    <dc:date>2010-03-17T12:12:26</dc:date>
    <upnp:albumArtURI dlna:profileID="PNG_TN" xmlns:dlna="urn:schemas-dlna-org:metadata-1-0">URL/albumart/22.png</upnp:albumArtURI>
    <upnp:icon>URL/albumart/22.png</upnp:icon>
    <dc:creator>land</dc:creator>
    <upnp:artist>sland</upnp:artist>
    <upnp:album>Change</upnp:album>
    <upnp:genre>Rock</upnp:genre>
    <res protocolInfo="http-get:*:audio/mpeg:DLNA.ORG_PN=MP3;DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01700000000000000000000000000000" size="9527987" duration="0:03:58">URL/1268.mp3</res>
    <res protocolInfo="http-get:*:image/png:DLNA.ORG_PN=PNG_TN;DLNA.ORG_CI=01;DLNA.ORG_FLAGS=00f00000000000000000000000000000" colorDepth="24" resolution="160x160">URL/albumart/22.png</res>
  </item>
  <item id="1269" parentID="20" restricted="1">
    <upnp:class>object.item.audioItem.musicTrack</upnp:class>
    <dc:title>Indian </dc:title>
    <dc:date>2010-03-17T12:06:32</dc:date>
    <upnp:albumArtURI dlna:profileID="PNG_TN" xmlns:dlna="urn:schemas-dlna-org:metadata-1-0">URL/albumart/13.png</upnp:albumArtURI>
    <upnp:icon>URL/albumart/13.png</upnp:icon>
    <dc:creator>MC</dc:creator>
    <upnp:artist>MC</upnp:artist>
    <upnp:album>manimal</upnp:album>
    <upnp:genre>Rap</upnp:genre>
    <res protocolInfo="http-get:*:audio/mpeg:DLNA.ORG_PN=MP3;DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01700000000000000000000000000000" size="8166707" duration="0:03:24">URL/1269.mp3</res>
    <res protocolInfo="http-get:*:image/png:DLNA.ORG_PN=PNG_TN;DLNA.ORG_CI=01;DLNA.ORG_FLAGS=00f00000000000000000000000000000" colorDepth="24" resolution="160x160">URL/albumart/13.png</res>
  </item>
  <item id="1277" parentID="20" restricted="1">
    <upnp:class>object.item.videoItem.movie</upnp:class>
    <dc:title>IronMan_TeaserTrailer-full_NEW.mpg</dc:title>
    <dc:date>2010-03-17T12:50:24</dc:date>
    <upnp:genre>Unknown</upnp:genre>
    <res protocolInfo="http-get:*:video/mpeg:DLNA.ORG_PN=(NULL);DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01700000000000000000000000000000" size="98926592" resolution="1920x1080" duration="0:02:30">URL/1277.mpg</res>
  </item>
</DIDL-Lite>

Here in the last Elemnt Item , there are few missing elements like icon,albumArtURi ..etc.

Now whhile Ity to access the Values by following LINQ to XML query,

var vAudioData = from xAudioinfo in xResponse.Descendants(ns + "DIDL-Lite").Elements(ns + "item").Where(x=>!x.Elements(upnp+"album").l

                                             orderby ((string)xAudioinfo.Element(upnp + "artist")).Trim()
                                             select new RMSMedia
{
                                                 strAudioTitle = ((string)xAudioinfo.Element(dc + "title")).Trim(),//((string)xAudioinfo.Attribute("audioAlbumcoverImage")).Trim()=="" ? ((string)xAudioinfo.Element("Song")).Trim():""
};

It show object reference is not set to reference of object. I do undersrand this is because of missing elements, is there any way I can pre-check if the elements exist , or check the number of elemnts inside item or any other way.

Any help is deeply appreciated.

Thanks,

Subhen

© Stack Overflow or respective owner

Related posts about xelement

Related posts about Xml