开发者

How to save data in xml file in windows phone 7

开发者_开发问答Hello Everyone, I am working on an application in which i need to save some data in IsolatedStorage . while my application is running i am able to see data from file. Once i close my application and restarting my application its not showing my data.

 public static IsolatedStorageFile isstore = IsolatedStorageFile.GetUserStoreForApplication();
 public static IsolatedStorageFileStream xyzStrorageFileStream = new IsolatedStorageFileStream("/category.xml", System.IO.FileMode.OpenOrCreate, isstore);


public static XDocument xmldoc = XDocument.Load("category.xml");
favouriteDoc.Save(rssFavouriteFileStream);
rssFavouriteFileStream.Flush();

Any one having any idea? How to do this?


In order to save structured data you need to use XML Writer or XML Serializer.

For example to save data:

using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("People2.xml", FileMode.Create, myIsolatedStorage))
    {
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        using (XmlWriter writer = XmlWriter.Create(isoStream, settings))
        {

            writer.WriteStartElement("p", "person", "urn:person");
            writer.WriteStartElement("FirstName", "");
            writer.WriteString("Kate");
            writer.WriteEndElement();
            writer.WriteStartElement("LastName", "");
            writer.WriteString("Brown");
            writer.WriteEndElement();
            writer.WriteStartElement("Age", "");
            writer.WriteString("25");
            writer.WriteEndElement();
            // Ends the document
            writer.WriteEndDocument();
            // Write the XML to the file.
            writer.Flush();
        }
    }
}

To read it back:

try
{
    using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
    {
        IsolatedStorageFileStream isoFileStream = myIsolatedStorage.OpenFile("People2.xml", FileMode.Open);
        using (StreamReader reader = new StreamReader(isoFileStream))
        {
            this.tbx.Text = reader.ReadToEnd();
        }
    }
}
catch
{ }

Answer is taken from this article, so all the credits go to WindowsPhoneGeek. Also, see other examples in the aforementioned article header.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜