I am trying to edit a local xml file in C# for my Windows Phone 8 app. On the web, I found countless examples using XmlDocument
using methods like AppendChild
. In Windows Phone 8, XmlDocument
has been replaced by XDocument
and AppendChild
disappeared. I tried the code below but get some error on protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
: The error can be seen here: http://i811.photobucket.com/albums/zz38/JelleK1996/cerror1_zpsb6aa5398.png
Can anyone help me out?
using 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;
using Microsoft.Phone.Shell;
using XmlLocalEdit1.Resources;
using System.Xml;
using System.Xml.Linq;
using System.Text;
namespace XmlLocalEdit1
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
try
{
StringBuilder sb = new StringBuilder();
XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = true;
xws.Indent = true;
using (XmlWriter xw = XmlWriter.Create(sb, xws))
{
XDocument xdoc = XDocument.Load("Resources/bar.xml");
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);
}
catch (Exception myExc)
{
Console.WriteLine(myExc.Message);
}
}
/*private static XElement CreateCocktail(XDocument xmlDoc,
string name,
int id)
{
var xmlCocktail = xmlDoc
}*/
}
}
Xml file:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<cocktail>
<name>43 Hedonism</name>
<id>14</id>
</cocktail>
<cocktail>
<name>B-52</name>
<id>4</id>
</cocktail>
</data>
All right, here a sample for you.
I strongly recommend you to use IsolatedStorage instead of editing the file in resources.
// copy the xml file to isolated storage
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!file.FileExists("bar.xml"))
{
StreamResourceInfo sr_en = Application.GetResourceStream(new Uri("Resources\\bar.xml", UriKind.Relative));
using (BinaryReader br_en = new BinaryReader(sr_en.Stream))
{
byte[] data = br_en.ReadBytes((int)sr_en.Stream.Length);
//Write the file.
using (BinaryWriter bw = new BinaryWriter(file.CreateFile("bar.xml")))
{
bw.Write(data);
bw.Close();
}
}
}
// work with file at isolatedstorage
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("bar.xml", FileMode.Open, file))
{
XDocument doc = XDocument.Load(stream, LoadOptions.None);
// add new node to data section
doc.Descendants("data").FirstOrDefault().Add(
new XElement("cocktail",
new XElement("name", "Dreamsicle"),
new XElement("id", 1)
)
);
// prevent xml file from doubling nodes
if (stream.CanSeek)
stream.Seek(0, SeekOrigin.Begin);
doc.Save(stream);
}
}
Your current code will add another <data>
element to the xml file, resulting an xml with multiple root elements which is not a valid xml format. I guess you want to add another <cocktail>
element to existing <data>
element instead. If this is the case, you can try this way :
.......
XDocument xdoc = XDocument.Load("Resources/bar.xml");
XElement xmlTree = new XElement("cocktail",
new XElement("name", "Dreamsicle"),
new XElement("id", 1)
);
//add new <cocktail> to existing root, which is <data> element
xdoc.Root.Add(xmlTree);
xdoc.Save(xw);
.......
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.