如何在Windows Phone 7中的xml文件中保存数据

时间:2011-09-12 11:12:40

标签: windows-phone-7

大家好,                  我正在开发一个应用程序,我需要在IsolatedStorage中保存一些数据。 当我的应用程序运行时,我能够从文件中查看数据。一旦我关闭我的应用程序并重新启动我的应用程序它不显示我的数据。

 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();

任何人有任何想法?怎么做?

1 个答案:

答案 0 :(得分:4)

为了保存结构化数据,您需要使用XML Writer或XML Serializer。

例如保存数据:

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();
        }
    }
}

要读回来:

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
{ }

答案来自this article,因此所有学分都归入WindowsPhoneGeek。另外,请参阅上述文章标题中的其他示例。