如何加载位于 Windows Phone 7 中应用程序文件夹内的 XML 文件?
我正在开发 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,确保 XML 文件的
构建操作
设置为内容
,并且复制到输出
选项设置为复制,如果更新
(或始终复制
)。然后,尝试以下操作:请注意,没有前导正斜杠 (/);几天前我花了几个小时陷入这个愚蠢的问题。
First, make sure the
Build Action
for your XML file is set toContent
and theCopy to output
option is set toCopy if newer
(orCopy always
). Then, try this:Note that there is no leading forward slash (/); I spent a few hours a few days ago stuck on this silly problem.