簡體   English   中英

從Windows Phone 8的隔離存儲中解析XML文件

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

為了進行實驗,我試圖創建一個新的XML文件並將其保存到隔離的存儲中,將其作為消息框顯示在應用程序中,打開並保存而不進行任何更改,然后再次顯示。

這是我要制作的XML:

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

這是我的代碼:

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

第一個ReadIsoStream()運行得很好。 一旦第二個ReadIsoStream()嘗試運行,我將收到以下錯誤消息:

附加信息:意外的XML聲明。 XML聲明必須是文檔中的第一個節點,並且不允許在其之前出現空格字符。 第2行,位置13。

請幫幫我?

就是說,文檔的第一個節點必須具有XML聲明。 這個->

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM