Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';无法在Silverlight中保存XDocument_C#_.net_Xml_Silverlight_Linq - Fatal编程技术网

C# Can';无法在Silverlight中保存XDocument

C# Can';无法在Silverlight中保存XDocument,c#,.net,xml,silverlight,linq,C#,.net,Xml,Silverlight,Linq,我想将Xdocument保存到xml文件中,silverlight似乎不接受xdoc.save的字符串类型(string str),因此我不得不在其中放入一个流,问题是当我放入一个文件流时,我收到一个错误,说“尝试访问该方法失败”,并且MethodeAccessException未处理。下面是我的代码: XDocument MainLBItems = XDocument.Load("SampleData/MainLBItems.xml"); Mai

我想将Xdocument保存到xml文件中,silverlight似乎不接受xdoc.save的字符串类型(string str),因此我不得不在其中放入一个流,问题是当我放入一个文件流时,我收到一个错误,说“尝试访问该方法失败”,并且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);

我只在WindowsPhone7中使用过Silverlight,但我怀疑桌面Silverlight也是如此。。。您不能像在完整的桌面.NET framework中那样直接使用文件。相反,你必须使用。例如:

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

(也可以使用streams调整其他代码。)

您可以将任意文件保存到独立存储中(请参见MSDN),也可以打开“另存为”对话框,请求用户访问以写入文件


从外观上看,您正在从XAP文件或XAP来自的服务器读取XML流。在这两种情况下,您都无法回写到这些地方。

@Gabe:不一定,不。应用程序能够加载自己的文件,但不能将任意数据保存到文件系统,这是非常有意义的。(老实说,我不知道
XDocument.Load
在Silverlight中到底能做什么。)您好,thx Jon,您的回答是,实际上是widows phone 7.1中的Silverlight,我忘了提到它,我试过你的代码,它说这是一个在IslatedStoregeFileStream中不存在的操作,你能给我解释一下我应该做什么吗do@Daoudi:首先,您应该提供更多的详细信息,例如引发异常的确切语句以及异常本身是否包含更多信息。这是在WP7中保存xml文件的正确方法,因此出现了一些问题。请注意,如果仍在子目录中使用文件名,则可能会导致问题。尝试一些simole诊断来找出发生了什么。谢谢你Jon我解决了它,这正是你所预测的,我在一个子目录中使用了一个文件名,再次使用thx