简体   繁体   English

读写XML文件

[英]Reading and writing to XML file

I have a Xamarin.forms shared project, I would like to write and read from a XML file which has to reside locally on each of the platform (android, iOS and windows). 我有一个Xamarin.forms共享项目,我想读写一个XML文件,该文件必须本地驻留在每个平台(Android,iOS和Windows)上。

I referred to this: Working with files , but I seem to be getting error : 我指的是: 处理文件 ,但似乎出现错误:

Error   2   The type or namespace name 'SharedPage' could not be found (are you missing a using directive or an assembly reference?)

at line: var assembly = typeof(SharedPage).GetTypeInfo().Assembly; 第一行: var assembly = typeof(SharedPage).GetTypeInfo().Assembly;

As I am new to Xamarin.forms I would also like to know where the files has to be placed for each of the platform project. 由于我是Xamarin.forms的新手,所以我也想知道每个平台项目必须将文件放置在何处。

In your code your are referencing the namespace as Test151.XML.WinPhone , however the real namespace you should be referring to is Test151.WinPhone.XML , as your WinPhone project has an assembly root of Test151.WinPhone . 在您的代码中,您将名称空间引用为Test151.XML.WinPhone ,但是您应该引用的实际名称空间是Test151.WinPhone.XML ,因为您的WinPhone项目具有程序集根目录Test151.WinPhone

So your have to change this within your TryFile.cs:- 因此,您必须在TryFile.cs中更改此设置:-

#if WINDOWS_PHONE
            //var resourcePrefix = "Test151.XML.WinPhone.";
            var resourcePrefix = "Test151.WinPhone.XML.";
#endif

Your also trying to get resource from the XML folder, of which you have not set the file as an EmbeddedResource , so you need to do that also. 您还尝试从XML文件夹中获取资源,但尚未将文件设置为EmbeddedResource ,因此您也需要这样做。

Further, your attempting to use the assembly of Xamarin.Forms to try and load the resource from, so where-as you had this:- 此外,您尝试使用Xamarin.Forms的程序集尝试Xamarin.Forms加载资源,因此-在您拥有的位置:-

var assembly = typeof(ContentPage).GetType().Assembly;

You need to change it to something along the lines of this:- (due to it being a SharedProject it will give you the correct assembly where to load the resource from. 您需要按照以下方式将其更改为:(由于它是一个SharedProject ,它将为您提供正确的程序集,从中可以从中加载资源。

    var assembly = typeof(TryFile).Assembly;

One last thing, your mentioning you want to read and write from an XML file. 最后一件事,您提到要从XML文件读取和写入。 This code part that you've implemented from the 'Working with files' page, you mentioned, will not help you as you are attempting to load an embedded resource, of which you can't write back, unless your aim is to write this to the file system afterwards? 您提到的从“使用文件”页面实现的此代码部分将无法为您提供帮助,因为您尝试加载无法回写的嵌入式资源,除非您打算编写此代码之后到文件系统?

Its the second half of the document you need to really be focusing on, if your aim is to accomplish reading and writing to an file in local storage. 如果您的目标是完成对本地存储中文件的读取和写入,则它实际上是文档的第二部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM