Question

I am making a Application on Windows Phone 8. The bit I am struggling with is getting the XML parsed. Here the XML File:

 <ArrayOfThemeParkList xmlns="http://schemas.datacontract.org/2004/07/WCFServiceWebRole1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <ThemeParkList>
       <id>1</id>
       <name>Alton Towers</name>
    </ThemeParkList>
    <ThemeParkList>
       <id>2</id>
       <name>Thorpe Park</name>
    </ThemeParkList>
    <ThemeParkList>
       <id>3</id>
       <name>Chessington World Of Adventures</name>
    </ThemeParkList>
    <ThemeParkList>
       <id>4</id>
       <name>Blackpool Pleasure beach</name>
    </ThemeParkList>
 </ArrayOfThemeParkList>

And the c# code that tries to parse it is:

void ThemeParksNames_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    //Now need to get that data and display it on the page
    //check for errors
    if (e.Error == null)
    {
        //No errors have been passed now need to take this file and parse it 
        //Its in XML format
        XDocument xdox = XDocument.Parse(e.Result);
        //need a list for them to be put in to
        List<ThemeParksClass> ParkList = new List<ThemeParksClass>();
        //Now need to get every element and add it to the list
        foreach (XElement item in xdox.Root.Elements("ThemeParkList"))
        {
            ThemeParksClass content = new ThemeParksClass();
            content.ID = Convert.ToInt32(item.Element("id").Value);
            content.ThemeParkName = item.Element("name").Value.ToString();
            ParkList.Add(content);
        }
        parkList.ItemsSource = ParkList.ToList();
    }
    else
    {
        //There an Error
    }
}

Now when using Break points it get to the for each loop but does not run at all just moves on. I am guessing i have the for each loop set wrong.

Many Thanks.

Was it helpful?

Solution

Your ThemeParkList elements are in a namespace http://schemas.datacontract.org/2004/07/WCFServiceWebRole1 - you'll need to adjust accordingly:

XNamespace ns = "http://schemas.datacontract.org/2004/07/WCFServiceWebRole1";
foreach (XElement item in xdox.Descendants(ns + "ThemeParkList"))

You'll need to handle the other elements in the same way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top