XML contents to Model in C# Windows Phone

Posted by Vuyiswamb under Mobile Development on 11/8/2013 | Points: 10 | Views : 1152 | Status : [Member] [MVP] [Administrator] | Replies : 1
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


<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


    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

  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

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


Thanks

Thank you for posting at Dotnetfunda
[Administrator]



Responses

Posted by: Vuyiswamb on: 11/8/2013 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
I dont know what to say , After i changed it to "Song" it worked nicely.

Thank you for posting at Dotnetfunda
[Administrator]

Vuyiswamb, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response