简体   繁体   English

从Windows Phone 8的隔离存储中解析XML文件

[英]Parsing XML file from Windows Phone 8's Isolated Storage

For the sake of experimentation, I'm trying to create a new XML file and save it to isolated storage, display it in the app as message box, open and save it with no changes made, then display it again. 为了进行实验,我试图创建一个新的XML文件并将其保存到隔离的存储中,将其作为消息框显示在应用程序中,打开并保存而不进行任何更改,然后再次显示。

This is the XML I'm trying to make: 这是我要制作的XML:

<?xml version="1.0" encoding="utf-8"?>
<houses />

This is my code: 这是我的代码:

public void InsertNewRoom()
{
    XDocument doc = null;
    CreateNewFile(doc); //create the file in isolated storage.
    ReadIsoStream(doc); //Display file as a new messagebox.
    OpenAndCloseFile(doc); //Open and save the file, no changes made.
    ReadIsoStream(doc); //Display file again.
}

public void OpenAndCloseFile(XDocument doc)
{
    using (IsolatedStorageFile isoStory = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("WizardResults.xml", FileMode.Open, isoStory))
        {

            doc = XDocument.Load(isoStream);
            doc.Save(isoStream);
        }
    }
    ReadIsoStream(doc);
}

public void CreateNewFile(XDocument doc)
{
    using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
    {
        //if (!isoStore.FileExists("WizardResults.xml"))
        {
            using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("WizardResults.xml", FileMode.Create, isoStore))
            {
                XDeclaration dec = new XDeclaration("1.0", "utf-8", "yes");
                doc = new XDocument(dec, new XElement("houses"));
                doc.Save(isoStream, SaveOptions.None);
            }
        }
    }
}
public void ReadIsoStream(XDocument doc)
{
    using (IsolatedStorageFile isoStory = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("WizardResults.xml", FileMode.Open, isoStory))
        {
            using (XmlReader reader = XmlReader.Create(isoStream))
            {
                XDocument xml = XDocument.Load(reader);

                MessageBox.Show(xml.ToString());
            }
        }
    }
}

The first ReadIsoStream() runs perfectly well. 第一个ReadIsoStream()运行得很好。 Once the second ReadIsoStream() attempts to run, I get this error message: 一旦第二个ReadIsoStream()尝试运行,我将收到以下错误消息:

Additional information: Unexpected XML declaration. 附加信息:意外的XML声明。 The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. XML声明必须是文档中的第一个节点,并且不允许在其之前出现空格字符。 Line 2, position 13. 第2行,位置13。

Help me, please? 请帮帮我?

It is saying that the first node of your document must have XML declarationg. 就是说,文档的第一个节点必须具有XML声明。 This one -> 这个->

<?xml version="1.0" encoding="utf-8"?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM