Results 1 to 3 of 3

Thread: [RESOLVED] XML contents to Model in C# Windows Phone

  1. #1

    Thread Starter
    Fanatic Member vuyiswamb's Avatar
    Join Date
    Jan 2007
    Location
    South Africa
    Posts
    829

    Resolved [RESOLVED] XML contents to Model in C# Windows Phone

    Good Day All

    i am trying to load an XML into a Model in WindowsPhone , so no DataTables , i tried the following last resort but it gives a Generic error that is not clear. The Following is the XML


    Code:
    <SONGS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <SONG>
        <SONG_ID>1</SONG_ID>
        <TITLE>SONG1</TITLE>
        <PRICE>3.95</PRICE>
        <URI>http://media.lakewood.org.edgesuite.net/JOM/podcast/mp3_audio/596_Podcast.mp3</URI>
      </SONG>
      <SONG>
        <SONG_ID>2</SONG_ID>
        <TITLE>SONG2</TITLE>
        <PRICE>3.95</PRICE>
        <URI>http://media.lakewood.org.edgesuite.net/JOM/podcast/mp3_audio/596_Podcast.mp3</URI>
      </SONG>
      <SONG>
        <SONG_ID>3</SONG_ID>
        <TITLE>SONG3</TITLE>
        <PRICE>R4.95</PRICE>
        <URI>http://media.lakewood.org.edgesuite.net/JOM/podcast/mp3_audio/596_Podcast.mp3</URI>
      </SONG>
      <SONG>
        <SONG_ID>4</SONG_ID>
        <TITLE>SONG4</TITLE>
        <PRICE>3.95</PRICE>
        <URI>http://media.lakewood.org.edgesuite.net/JOM/podcast/mp3_audio/596_Podcast.mp3</URI>
      </SONG>
      <SONG>
        <SONG_ID>1</SONG_ID>
        <TITLE>SONG5</TITLE>
        <PRICE>6.95</PRICE>
        <URI>http://media.lakewood.org.edgesuite.net/JOM/podcast/mp3_audio/596_Podcast.mp3</URI>
      </SONG>
      <SONG>
        <SONG_ID>5</SONG_ID>
        <TITLE>SONG6</TITLE>
        <PRICE>10.95</PRICE>
        <URI>http://media.lakewood.org.edgesuite.net/JOM/podcast/mp3_audio/596_Podcast.mp3</URI>
      </SONG>
    </SONGS>
    and my model is defined like this


    Code:
        public class SongsModel
        {
            public int SONG_ID { get; set; }
            public string TITLE { get; set; }
            public string URI { get; set; }
            public string PRICE { get; set; }
        }
    and the Function is

    Code:
      public static List<SongsModel> GetSongs()
             {
                  
                 //Get the XML and Convert it to DataSet 
                 XDocument loaded = XDocument.Load("MUSIC.XML");
                 List<SongsModel> songs = (from c in loaded.Descendants("SONGS")
                                           select new SongsModel()
                                           {
                                               SONG_ID = (int)c.Element("SONG_ID"),
                                               TITLE = (string)c.Element("TITLE"),
                                               URI = (string)c.Element("URI"),
                                               PRICE = (string)c.Element("PRICE")
                                           }).ToList<SongsModel>();
                 return songs;
             }
    and i get the following Error , a Generic Windows Phone Exception

    Code:
    An unhandled exception of type 'System.ArgumentNullException' occurred in System.Windows.ni.dll


    Thanks
    Last edited by vuyiswamb; Nov 8th, 2013 at 12:50 PM.

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: XML contents to Model in C# Windows Phone

    This should work:

    Code:
    List<SongsModel> songs = loaded.Root.Elements("SONG").Select(x => new SongsModel()
                {
                    SONG_ID = (int)x.Element("SONG_ID"),
                    TITLE = (string)x.Element("TITLE"),
                    PRICE = (string)x.Element("PRICE"),
                    URI = (string)x.Element("URI")
                }).ToList();
    But after getting it working, I just realised that the reason yours isn't working is because you're getting the descendants "SONGS" instead of "SONG".

  3. #3

    Thread Starter
    Fanatic Member vuyiswamb's Avatar
    Join Date
    Jan 2007
    Location
    South Africa
    Posts
    829

    Re: XML contents to Model in C# Windows Phone

    I dont know what to say , After i changed it to "Song" it worked nicely. If you were around i would buy you a drink. Thanks buddy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width