I am trying to edit a local XML file in C # for my Windows Phone 8 app. On the Web, I've found countless examples using XmlDocument
by using methods like AppendChild
in Windows Phone 8, XmlDocument
to XDocument is lost by
and AppendChild
. I tried the code below but got some error on this protected override zero OnNavigatedTo (System.Windows.Navigation.NavigationEventArgs e)
: Error can be seen here:
Can anyone help me out?
using the system; Using System.Collections.Generic; Using System.Linq; Using System.Net; Using System.Windows; Using System.Windows.Controls; Using System.Windows.Navigation; Using Microsoft.Phone.Controls; Use of Microsoft phone. Shell; Using XmlLocalEdit1.Resources; Using System.Xml; Using System.Xml.Linq; Using System.Text; Namespace XmlLocalEdit1 {Public Partial Class Home Page: Phone Application Page {// Constructor Public Menpage () {InitializeComponent (); // ApplicationBar // BuildLocalizedApplicationBar () sample code for localization; } To upgrade protected override zero (System.Windows.Navigation.NavigationEventArgs E) {try {StringBuilder sb = new StringBuilder (); XmlWriterSettings xws = New XmlWriterSettings (); Xws.OmitXmlDeclaration = True; Xws.indent = true; (XmlWriter xw = XmlWriter.Create (sb, xws)) using {XDocument xdoc = XDocument.Load ("resource / bar.exml"); XElement xmlTree = New XElement ("Data", New XElement ("Cocktail", New XElement ("Name", "Dreamsicle"), New XElement ("ID", 1))); Xdoc.Add (xmlTree); Xdoc.Save (xw); } //xdoc.Add (xmlTree); //xdoc.Save("Resources/bar.xml ", SaveOptions.None); } Hold (exception myExc) {Console.WriteLine (myExc.Message); }} / * Private Static XElement CreateCocktail (XDocument xmlDoc, String Name, Integer ID) {var xmlCocktail = xmlDoc} * /}}
Xml file:
< Code> & lt ;? XML version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Data & gt; & Lt; Cocktail & gt; & Lt; Name & gt; 43 Headonisms & lt; / Name & gt; & Lt; ID & gt; 14 & lt; / Id & gt; & Lt; / Cocktail & gt; & Lt; Cocktail & gt; & Lt; Name & gt; B-52 & lt; / Name & gt; & Lt; ID & gt; 4 & lt; / Id & gt; & Lt; / Cocktail & gt; & Lt; / Data & gt;
OK, here's a sample for you.
I strongly recommend you to use IsolatedStorage instead of editing the file in the resources. Copy the XML file for different storage using
// (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication ()) {if file! FileExists ("bar.xml")) {StreamResourceInfo Sr_en = Application.GetResourceStream (new URI ("resource \\ bar.exml", uricind.release)); (Using binaryreader br_en = new binaryreader (sr_en.stream)) {byte [] data = br_en.ReadBytes ((int) sr_en.stream.Length); // Write the file (Binrrritter BW = new biirirter (using file.crylatefile ("bar.exml")) {BWWright (data); Bw.Close (); }}} // IsolatedStorage (IsolatedStorageFileStream Section = New IsolatedStorageFileStream ("bar.xml", FileMode.Open, File)) using IsolatedStorage with File (); {XDocument Dock = XDocument.Load (Section, LoadOptions.None); // New node data section doc.Descendants ("Data") to "Dreamsicle" Add new XElement ("ID", 1. Add FirstOrDefault (). (New XElement ("Cocktail", New XElement ("name",), ))); // Stop the XML file from doubling the nodes if the stream. Seek (stream.CanSeek) (0, SeekOrigin.Begin); Doc.Save (stream); }}
Comments
Post a Comment