如何加载位于 Windows Phone 7 中应用程序文件夹内的 XML 文件?

发布于 2024-10-09 11:06:23 字数 1397 浏览 0 评论 0原文

我正在开发 Windows Phone 7 应用程序。我是 Windows Phone 7 应用程序的新手。我通过右键单击项目并在我的项目中添加了 XML 文件选择添加->新项目。然后,我可以使用以下代码轻松地将 XML 文件加载到我的应用程序中

IsolatedStorageFile isfData = IsolatedStorageFile.GetUserStoreForApplication();
            XDocument doc = null;
            IsolatedStorageFileStream isfStream = null;
            if (isfData.FileExists(strXMLFile))
            {
                isfStream = new IsolatedStorageFileStream(strXMLFile, FileMode.Open, isfData);
                doc = XDocument.Load(isfStream);
                isfStream.Close();
            }
            else
            {
                doc = XDocument.Load(strXMLFile);
                isfStream = new IsolatedStorageFileStream(strXMLFile, FileMode.CreateNew, isfData);
                doc.Save(isfStream);
                isfStream.Close();
            }

通过使用上面的代码,我可以执行读取和读取操作。我的 XML 文件中的写入操作。

但是当我将 XML 文件放入该文件夹时,问题就出现了。我的问题如下: 我通过右键单击项目名称并在项目中添加了一个名为“XML Files”的文件夹选择添加->视觉工作室中的新文件夹。然后我通过右键单击文件夹并将 XML 文件添加到“XML 文件”文件夹中。选择添加->新项目。当我将 XML 文件放入文件夹时,我无法将其加载到我的应用程序中。我还尝试使用以下语句,

isfStream = new IsolatedStorageFileStream("/XML Files/"+strXMLFile, FileMode.Open, isfData);

但出现错误

doc = XDocument.Load(strXMLFile);

“在应用程序 xap 包中找不到文件 '/XML Files/A.xml'”。我应该怎么办 ?如何加载文件夹内的 XML 文件?我的代码有什么问题吗?您能给我提供任何可以解决上述问题的代码或链接吗?

I am developing window phone 7 application. I am new to the window phone 7 application. I have added XML file in my project by right clicking the project & selecting the Add -> New Item. I can then easily load the XML file in my application by using the following code

IsolatedStorageFile isfData = IsolatedStorageFile.GetUserStoreForApplication();
            XDocument doc = null;
            IsolatedStorageFileStream isfStream = null;
            if (isfData.FileExists(strXMLFile))
            {
                isfStream = new IsolatedStorageFileStream(strXMLFile, FileMode.Open, isfData);
                doc = XDocument.Load(isfStream);
                isfStream.Close();
            }
            else
            {
                doc = XDocument.Load(strXMLFile);
                isfStream = new IsolatedStorageFileStream(strXMLFile, FileMode.CreateNew, isfData);
                doc.Save(isfStream);
                isfStream.Close();
            }

By using the above code I can perform the read & write operation in my XML file.

But the problem arises when I put my XML file into the folder. My problem is as follows:
I have added one folder named 'XML Files' in my project by right clicking the project name & selecting the Add -> New Folder in the visual studio. Then I have added a XML file into the 'XML Files' Folder by right clciking the folder & selecting the Add->New Item. When I put the XML file into the folder I am not able to Load it in my application. I have also tried with the following statement

isfStream = new IsolatedStorageFileStream("/XML Files/"+strXMLFile, FileMode.Open, isfData);

I am getting error at

doc = XDocument.Load(strXMLFile);

I am getting the error "Cannot find file '/XML Files/A.xml' in the application xap package." What should I do ? How to load the XML file loacted inside folder ? Is anything wrong in my code ? Can you please provide me any code or link through which I can resolve the above issue ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凡尘雨 2024-10-16 11:06:23

首先,确保 XML 文件的构建操作设置为内容,并且复制到输出选项设置为复制,如果更新(或始终复制)。然后,尝试以下操作:

XDocument doc = XDocument.Load( "XML Files/MyXmlFile.xml" );

请注意,没有前导正斜杠 (/);几天前我花了几个小时陷入这个愚蠢的问题。

First, make sure the Build Action for your XML file is set to Content and the Copy to output option is set to Copy if newer (or Copy always). Then, try this:

XDocument doc = XDocument.Load( "XML Files/MyXmlFile.xml" );

Note that there is no leading forward slash (/); I spent a few hours a few days ago stuck on this silly problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文