I need to read a xml file, but I have this error when try to read it.
public void load()
{
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream fileStream;
try
{
fileStream = myIsolatedStorage.OpenFile("sites.xml", FileMode.Open,
FileAccess.Read);
}
catch (Exception)
{
fileStream = myIsolatedStorage.CreateFile("sites.xml");
XDocument sites= new XDocument(new XElement("root"));
fileStream = myIsolatedStorage.OpenFile("sites.xml", FileMode.Open,
FileAccess.Read);
}
List<Site> listSites= new List<Site>();
using (StreamReader file= new StreamReader(fileStream))
{
XDocument doc= XDocument.Load(file);
foreach (XElement elemento in doc.Elements())
{
String name= elemento.Element("name").Value;
String url = elemento.Element("url").Value;
listSites.Add(new Site(name));
}
lstSites.ItemsSource = listSites;
}
}
On the line
XDocument doc= XDocument.Load(file);
I receive the error in the subject. Can you help me ?
Maybe you should change the way you are working with xml files and isolated storage. you could create a Site class containing your name and url properties and serialize a list of these to the isolated storage.
an example can be found here:
http://www.windowsphonegeek.com/tips/All-about-WP7-Isolated-Storage—Read-and-Save-XML-files