Get started with Metro style app development-Examples of file access operations for Operating Metro style apps, such as reading, modifying, and saving XML files

Source: Internet
Author: User

Reading, modifying, and saving XML files in the Metro style app

Use the xml api of win RT to operate XML files. Select an XML node to load the XML file, modify the attributes of the XML node, and save the XML file. There are also many asynchronous operations (async, await ).

For the running environment, refer to the previous article: file access operation example of Metro style app

Is the API of the metro style app.

Next, let's talk about the specific operations:

1,

Storagefile = NULL; async void Init () {// load the XML file xmldocument Doc = await loadxmlfile (@ "XML \ xslttransform", "xmlcontent. XML "); // getxml () Get the XML text string MSG = Doc. getxml (); // display the text myricheditbox in richeditbox. document. settext (windows. UI. text. textsetoptions. none, MSG); var XPath = "/library/book/chapter/section/Paragraph/@ A"; // select the node value var aattributes = Doc. selectnodes (XPath); For (uint I = 0; I <aattributes. length; I ++) {// modify the attributes of the node aattributes. item (I ). nodevalue = "newvalue";} // save the file. The path to modify and save the file is the system installation path. Doc. savetofileasync (storagefile );}

To load an XML file, perform the following operations:

The current path is the local path for installation. In this example, the path is bin \ debug \ filename \ xmlfile.

        /// <summary>        /// Load Xml file        /// </summary>        private async Task<XmlDocument> LoadXmlFile(string folder, string file)        {            StorageFolder storageFolder = await Package.Current.InstalledLocation.GetFolderAsync(folder);            storageFile = await  storageFolder.GetFileAsync(file);            XmlLoadSettings loadSettings = new XmlLoadSettings();            loadSettings.ProhibitDtd = false;            loadSettings.ResolveExternals = false;            return await XmlDocument.LoadFromFileAsync(storageFile, loadSettings);        }

 Note: loadsettings. prohibitdtd = false; loadsettings. resolveexternals = false; Security Settings must be set to false; otherwise, an exception occurs. There have also been some changes in file access in win 8, using storagefolder and so on.

The XML file is as follows:

<library>  <book>    <chapter></chapter>    <chapter>      <section>        <paragraph a="b">1</paragraph>        <paragraph a="b">2</paragraph>      </section>    </chapter>  </book></library>

 

Modified XML file:

<library>  <book>    <chapter></chapter>    <chapter>      <section>        <paragraph a="newValue">1</paragraph>        <paragraph a="newValue">2</paragraph>      </section>    </chapter>  </book></library>

 

2. Modify the value of a node

var xnodepath = "/library/book/chapter/section/paragraph";
var nodeList = doc.SelectNodes(xnodepath);for (uint i = 0; i < nodeList.Length; i++){      IXmlNode node = nodeList.Item(i);      if (node.Attributes[0].NodeValue.ToString() == "one")      {             node.FirstChild.NodeValue = "New node Value";      }             }

The above is the node value for modifying the node attribute to one.

 

Summary: There are some differences between Windows RT and XML operations.

The following are the XML operations I summarized previously. What are the differences between them:

The following describes the specific operations on XML in C #.

How to read and write XML documents in. net

C # operating XML selectnodes, selectsinglenode always returns NULL and XPath Introduction

C # Use selectsinglenode to parse XML files with multiple namespaces

 

The above is just my learning experience. If you have any opinions or suggestions, you are still studying and researching. You are welcome to join us in learning and making progress together.

 

 

Leave a question for you:

How to obtain node information when the namespace attribute ('HTTP: // www.microsoft.com ') is added to the XML file;

XML files such:

<library xmlns='http://www.microsoft.com'>  <book>    <chapter></chapter>    <chapter>      <section>        <paragraph a="one">1</paragraph>        <paragraph a="two">2</paragraph>      </section>    </chapter>  </book></library>

How can I use the doc. selectnodesns method?

A:

Doc. loadxml (· "<urlset xmlns = 'HTTP: // www.foo.com '> <URL xmlns = 'HTTP: // www.bar.com '> <loc> http://www.far.com </loc> </URL> </urlset> "); windows. foundation. collections. propertyset NS = new windows. foundation. collections. propertyset (); NS. add ("foo", "http://www.foo.com"); NS. add ("bar", "http://www.bar.com"); node = Doc. selectsinglenodens ("/FOO: urlset/Bar: URL/Bar: Loc", NS); or Doc. loadxml (· "<urlset xmlns = 'HTTP: // www.foo.com '> <URL xmlns = 'HTTP: // www.bar.com '> <loc> http://www.far.com </loc> </URL> </urlset> "); node = Doc. selectsinglenodens ("/FOO: urlset/Bar: URL/Bar: Loc", "xmlns: Foo = 'HTTP: // www.foo.com 'xmlns: bar = 'HTTP: // www.bar.com '");

References

Related Article

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.