I am downloading a xml file from the internet and save it in isolated storage. If I try to read it I get an error:
Data at the root level is invalid. Line 1, position 1.
string tempUrl = "http://xxxxx.myfile.xml"; // changed
WebClient client = new WebClient();
client.OpenReadAsync(new Uri(tempUrl));
client.OpenReadCompleted += new OpenReadCompletedEventHandler(delegate(object sender, OpenReadCompletedEventArgs e) {
StreamWriter writer = new StreamWriter(new IsolatedStorageFileStream("myfile.xml", FileMode.Create, FileAccess.Write, myIsolatedStorage));
writer.WriteLine(e.Result);
writer.Close();
});
This is how I download and save the file…
And I try to read it like that:
IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("myfile.xml", FileMode.Open, FileAccess.Read);
XDocument xmlDoc = XDocument.Load(fileStream);
This is where I get the error…
I have no problem reading the same file without downloading and saving it to isolated storage… so there must be the fault.
This:
doesn’t do what you think it does. It’s just calling
ToString()on aStream, and writing the result to a file.I suggest you avoid using a
StreamWritercompletely, and simply copy frome.Resultstraight to theIsolatedStorageFileStream:where
CopyStreamwould be a method to just copy the data, e.g.