views:

23

answers:

2

I am using the following methods to download an xml file

private void LoadXMLFile()
{
  WebClient xmlClient = new WebClient();
  xmlClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(XMLFileLoaded);
  xmlClient.DownloadStringAsync(new Uri("chart.xml", UriKind.RelativeOrAbsolute));
}

void XMLFileLoaded(object sender, DownloadStringCompletedEventArgs e)
{
  if (e.Error == null)
  {
    string xmlData = e.Result;
    HtmlPage.Window.Alert(xmlData);
    x2 = new XDocument(xmlData);
  }
} 

I want to use the information inside xmlData to build an xDocument, like I am trying to do in my last line. It does not give any errors but my program does not work so I must not be correctly making the xDocument. Assiging an xml document directly to x2 like this

x2 = Xdocument.Load("chart.xml")

works.

But I need to do it through webclient. what am I doing wrong here

A: 

Once you've got the xmlData string, it's easy - use XDocument.Parse:

XDocument doc = XDocument.Parse(xmlData);

Could you elaborate why you need to use WebClient rather than XDocument.Load though? Is it to make the call asynchronous?

Jon Skeet
Parse is not working. This xml is supposed to build a silverlight app but on using webclient and parse the app just gets stuck at 100% loading, the same file added as a link from my web project's client bin and loaded using xDocument.Load works... but I need to do it through web client becase the link and load method does not update the xap
xdocsl
@xdocsl: If `Parse` is not working, then the XML may not be valid, or may be being served with the wrong wrong content encoding (which would certainly mess things up). If you try to step through the code with a debugger, what happens when you use `Parse`?
Jon Skeet
ok I was having problems syncronizing. The web client was downloading the file but the other methods supposed to use the file were being called before the file was fully downloaded
xdocsl
+1  A: 

XDocument.Parse

gandjustas