"Win 10 app development" embeds files into an XML document

Source: Internet
Author: User
Tags cdata

To embed (or deposit) the contents of a file into an XML document, it is believed that many friends would think of converting a byte array into a Base64 string, and then as a node of an XML document. However, one would say that the converted Base64 string contains symbols such as "+", and parsing errors will occur in writing to the XML document.

I don't know if you think of it. XML documents have a CDATA content node that ignores parsing of characters, and uses CDATA nodes to hold base64 strings.

It's not hard to do this in the win app.

There are several ways to read and write XML documents, either using classes in. NET Core or LINQ to XML, or by using the types of operations available in the run-time APIs. It doesn't matter which one you like to use.

The following old week, as an example of the run-time API, demonstrates how to embed the contents of a file into an XML document.

Really not complicated, on the code you know.

            //Select FileFileopenpicker Picker =NewFileopenpicker (); Picker. Filetypefilter.add ("*");//All FilesStorageFile file =awaitPicker.            Picksinglefileasync (); if(File = =NULL)            {                return; }            //Store file name            stringFileName =file.            Name; //File Size            ULONGLen = (awaitfile. Getbasicpropertiesasync ()).            Size; //Base64 String            stringB64datastr =NULL; //Read File contents            using(DataReader reader =NewDataReader (awaitfile. OpenReadAsync ())) {awaitReader. LoadAsync ((UINT) len); IBuffer Buffer=Reader. Readbuffer (reader.                Unconsumedbufferlength); //Calculate Base64 StringB64datastr =cryptographicbuffer.encodetobase64string (buffer); }            //constructing an XML documentXmlDocument XML =NewXmlDocument (); //root nodeXmlElement root = XML. CreateElement ("FileInfo"); Xml.            AppendChild (root); //the node that holds the file nameXmlElement Namenode = XML. CreateElement ("name"); Namenode. AppendChild (XML.            createTextNode (FileName)); Root.            AppendChild (Namenode); //File Content nodeXmlElement Datanode = XML. CreateElement ("content"); Datanode. AppendChild (XML.            Createcdatasection (B64DATASTR)); Root.            AppendChild (Datanode); //displaying XML contentTbxml.text =XML.            GETXML (); //Save the XML document to the documentStoragefolder doclib =knownfolders.documentslibrary; StorageFile NewFile=awaitDocLib. Createfileasync ("Test.xml", creationcollisionoption.replaceexisting); awaitXML.            Savetofileasync (NewFile); Windows.UI.Popups.MessageDialog D=NewWindows.UI.Popups.MessageDialog ("saved successfully. "); awaitD.showasync ();

The key points are these lines of code:

            //constructing an XML documentXmlDocument XML =NewXmlDocument (); //root nodeXmlElement root = XML. CreateElement ("FileInfo"); Xml.            AppendChild (root); //the node that holds the file nameXmlElement Namenode = XML. CreateElement ("name"); Namenode. AppendChild (XML.            createTextNode (FileName)); Root.            AppendChild (Namenode); //File Content nodeXmlElement Datanode = XML. CreateElement ("content"); Datanode. AppendChild (XML.            Createcdatasection (B64DATASTR)); Root. AppendChild (Datanode);

The function of these lines of code is to build the structure of the XML document in memory.

Finally, let's see what happens after the file is embedded in the XML.

Finally, the size of the file is not suitable for embedding into XML, the reason why I do not have to explain more.

Sample code Download: Http://files.cnblogs.com/files/tcjiaan/putfiletoxmldoc.zip

"Win 10 app development" embeds files into an XML document

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.