简体   繁体   中英

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).

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;

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.

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 .

So your have to change this within your 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.

Further, your attempting to use the assembly of Xamarin.Forms to try and load the resource from, so where-as you had this:-

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.

    var assembly = typeof(TryFile).Assembly;

One last thing, your mentioning you want to read and write from an XML file. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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