wp8.1 Study11:app File read-write and use XML and JSON serialization

Source: Internet
Author: User

I. File reading and writing

1. Basic operation (using FileIO API)

This method has been learned in the previous Stduy, then post it to review, the code is as follows:

Private Async voidWritetexttolocalstoragefile (stringFileNamestringtext) {            varfold = Windows.Storage.ApplicationData.Current.LocalFolder;//Open FolderStorageFile file =awaitFold. Createfileasync (filename, creationcollisionoption.replaceexisting);//Create a file            awaitFileio.writetextasync (file, text);//write content in a file        }        Private Asynctask<string> Readtextfromlocalstorage (stringfilename) {            varfold = Windows.Storage.ApplicationData.Current.LocalFolder;//Open FolderStorageFile file =awaitFold. Getfileasync (filename);//Open the corresponding file            stringresult =awaitFileio.readtextasync (file);//read the contents of the file            returnresult; }

Note: All file operations are asynchronous

2. Using stream operation

We can also create a stream to use for read and write operations. It can be used on all WINRT.

To write, the code is as follows:

StreamWriter Writestream =NULL; Private Async voidOpenlocalstoragefile (stringfilename)//Open file stream {if(Writestream! =NULL)return; Storagefolder Localfolder=Windows.Storage.ApplicationData.Current.LocalFolder; Stream BaseStream=awaitlocalfolder.openstreamforwriteasync (filename, creationcollisionoption.openifexists); Writestream=NewStreamWriter (BaseStream); }        Private Async voidcloselocalstoragefile ()//close file stream {if(Writestream = =NULL)return; awaitWritestream.flushasync ();            Writestream.dispose (); Writestream=NULL; }        Private Async voidWriteLine (stringtext)//write data to the file stream {awaitWritestream.writeasync (text +"\ n"); }

Ii. using XML, JSON serialization (serializer)

Through previous studies, we have been able to read and write some settings for saving and text, but using XML, JSON serialization, we can also easily store objects. The serialized library on the Windows Phone makes it very easy to store structured data. The application can serialize the data into XML or JSON format, and the data will be stored or converted to text.

1. Serialization rules

    • The object required for XML serialization must have a parameterless constructor.
    • Private and static members will not be saved.
    • If you add members to the corresponding class when the application is updated, the application will get an error when recovering the serialized data file that uses the previous class definition.

2. Using JSON serialization

The following serializes a customers object and converts the data into a JSON file save.

Write operation:

using await notesfolder.openstreamforwriteasync (filename,     creationcollisionoption.openifexists)) {    =         New DataContractJsonSerializer (typeof(Customers));    Serializer. WriteObject (stream, customers);

Await stream. Flushasync ();

Stream. Dispose ();

}

Read operation:

using await notesfolder.openstreamforreadasync (filename)) {    =         New DataContractJsonSerializer (typeof(Customers));      as Customers;
Stream. Dispose ();//Release data stream}

3. Using XML serialization

Operations are similar to JSON serialization, the following code is:

Write operation:

New XmlSerializer (typeof(Customers)); serializer. Serialize (stream, customers);

Read operation:

New XmlSerializer (typeof as Customers;

Knowledge Learning Sharing ~ ~ ~ Small white One, the above information is basically a translation channel9 inside the relevant courses.

wp8.1 Study11:app File read-write and use XML and JSON serialization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.