簡體   English   中英

無法在Silverlight中保存XDocument

[英]Can't Save XDocument in Silverlight

我想將Xdocument保存到xml文件,似乎silverlight不接受xdoc.save(string str)的字符串類型,因此我不得不在其中放入一個流,問題是當我放置一個filestream我收到一條錯誤消息,提示“嘗試訪問該方法失敗”,而這里未處理MethodeAccessException是我的代碼:

XDocument MainLBItems = XDocument.Load("SampleData/MainLBItems.xml");            
            MainLBItems.Root.Add(new XElement("Item",
                                            new XElement("Title", this.tb_Title.Text),
                                            new XElement("Dscrp", this.tb_Dscrp.Text),
                                            new XElement("Count", "0")));
            FileStream fs = new FileStream("SampleData/MainLBItems.xml", FileMode.Open, FileAccess.Write);
            MainLBItems.Save(fs);

我只在Windows Phone 7中使用過Silverlight,但我懷疑這同樣適用於桌面Silverlight ...您不能像在完整的.NET框架中那樣直接使用文件。 相反,您必須使用隔離存儲 例如:

using (var file = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (var stream = file.OpenFile("file.xml", FileMode.Create))
    {
        document.Save(stream);
    }
}

(也可以使用流調整其他代碼。)

您可以將任意文件保存到隔離的存儲中( 例如,請參見MSDN),也可以打開“另存為”對話框,要求用戶具有對文件的寫入權限。

從外觀上看,您正在從XAP文件或XAP所在的服務器讀取XML流。 無論哪種情況,您都無法寫回那些地方。

暫無
暫無

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

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