Windows Phone App needs to read in from xml source

Issues with trying to fill a list with values from an XML source.

I have a WebClient to read into the project which calls the function below.

WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
Uri url = new Uri("http://www.usi.edu/webservices/iphone/USIINFOterms.xml", UriKind.Absolute);
client.OpenReadAsync(url);

The function

public void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    try
    {
        var xml = XDocument.Load(e.Result);

        var results = from row in xml.Element("value").Elements() select row;
        // now I loop all rows and print the title; of course you canfc
        // do other stuff here or combine some data processing with the LINQ above
        // - this is up to you
        foreach (var result in results)
        {
            item.ItemLine1 = result.Value;
            list.Add(item);
         }
     }
     catch (Exception c)
     {
         MessageBox.Show(c.Message);
     }
}

The list elements return a null reference.

Here's what the XML file looks like

<data>
 <row>
  <value>Fall 2013</value> 
  <value>201410</value> 
 </row>
 <row>
  <value>Third Summer 2013</value> 
  <value>201350</value> 
 </row>

Not having too much luck with the xml file. Any help is appreciated.

Answers


A couple of thoughts:

  1. Replace XDocument.Load(e.Result) with XDocument.Parse(e.Result). XDocument.Load() treats is argument as a URI, and you have -- I assume -- already downloaded the XML document. XDocument.Parse() cracks open that document for you.

  2. Try the following:

    var results = from row in xml.Root.Elements("row")
                  select row.Element("value");
    

    XElement.Elements() only works from the current XML element -- it doesn't scan the entire XML document tree. To scan the entire tree, you can use XElement.Descendants(). Or, to more precisely pick the elements you need, use the pattern as above -- pick elements from the first level, then go deeper into the next level.


Need Your Help

Passing a specific shape as a function argument

variables powerpoint argument-passing shapes

Using Powerpoint 2010 and MS Visual Basic for applications:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.