Edit XML file in the Windows Store application

I am making an application in a Windows repository and have a problem writing xml in the generated XML file. I did this XML file editing in the Windows Store application , but it did not work for me.

I want this xml to be written in my xml file at the click of a button.

Any alternative method for this material.

<drink> <drinkImage>ck.png</drinkImage> <drinkTitle>COKE</drinkTitle> <drinkDescription>(1793-1844)</drinkDescription> </drink> 

My current file is:

  <?xml version="1.0" encoding="utf-8" ?> <drinks> <drink> <drinkImage>pepsi.png</drinkImage> <drinkTitle>PEPSI</drinkTitle> <drinkDescription>(1793-1844)</drinkDescription> </drink> **<here I Want above xml on button click>** </drinks> 

Here is what I tried:

 namespace DrinksApp { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> public sealed partial class coke : Page { public coke() { this.InitializeComponent(); } XmlDocument dom = new XmlDocument(); private void Button_Click(object sender, RoutedEventArgs e) { this.Frame.Navigate(typeof(softdrinks)); } private async void Button_Click_1(object sender, RoutedEventArgs e) { XDocument xmlDoc = XDocument.Load("favourite//fav.xml"); xmlDoc.Root.Add(new XElement("drink", new XAttribute("drinkImage","ck.png"), new XAttribute("drinkTitle","PEPSI"), new XAttribute("drinkDescription","NONE") )); xmlDoc.Save(xmlDoc); **//This isn't working in windows store ..** } } } } 

I already have an xml file as above:

+7
c # xml windows-store-apps xmldocument
source share
3 answers

Perhaps this is the right way (not tested in the Windows Store project). Pay attention to which element is added to which parent element and which inner text is assigned to any element where the source codes are incorrect (I even evaluate the effort):

 var existingRoot = dom.DocumentElement; var newElement = dom.CreateElement("drink"); existingRoot.AppendChild(newElement); var drinkImageElement = dom.CreateElement("drinkImage"); drinkImageElement.InnerText = "ck.png"; newElement.AppendChild(drinkImageElement); var drinkTitleElement = dom.CreateElement("drinkTitle"); drinkTitleElement.InnerText = "COKE"; newElement.AppendChild(drinkTitleElement); var drinkDescriptionElement = dom.CreateElement("drinkDescription"); drinkDescriptionElement.InnerText = "Nothing"; newElement.AppendChild(drinkDescriptionElement); 

Here is a link to an online demo (console applications): https://dotnetfiddle.net/oElYWJ

output:

 <drinks> <drink> <drinkImage>pepsi.png</drinkImage> <drinkTitle>PEPSI</drinkTitle> <drinkDescription>(1793-1844)</drinkDescription> </drink> <drink> <drinkImage>ck.png</drinkImage> <drinkTitle>COKE</drinkTitle> <drinkDescription>Nothing</drinkDescription> </drink> </drinks> 
+4
source share

You cannot save an XML document here. All files in the application package are read-only. You must save your XML document in localState:

  public async Task DoJobAsync() { XDocument xmlDoc = XDocument.Load("favourite//fav.xml"); xmlDoc.Root.Add(new XElement("drink", new XAttribute("drinkImage", "ck.png"), new XAttribute("drinkTitle", "PEPSI"), new XAttribute("drinkDescription", "NONE") )); using (var stream = await (await ApplicationData.Current.LocalFolder.CreateFileAsync("fav.xml")).OpenAsync(FileAccessMode.ReadWrite)) { xmlDoc.Save(stream.AsStreamForWrite()); } } 
+3
source share

Wrote this in a console application, but it should still work for the Windows Store application.

  XDocument xdoc = XDocument.Load("fa.xml"); xdoc.Root.Add(new XElement("drink", new XElement("drinkImage", "Img.png"), new XElement("drinkTitle", "NewDrink"), new XElement("drinkDescription", "HelloWorld"))); xdoc.Save("fa.xml"); 

Output:

 <?xml version="1.0" encoding="utf-8"?> <drinks> <drink> <drinkImage>pepsi.png</drinkImage> <drinkTitle>PEPSI</drinkTitle> <drinkDescription>(1793-1844)</drinkDescription> </drink> <drink> <drinkImage>Img.png</drinkImage> <drinkTitle>NewDrink</drinkTitle> <drinkDescription>HelloWorld</drinkDescription> </drink> </drinks> 

Your original code is very close to what you wanted. You used "XAttribute", not "XElement". This is not a problem, but the result will look like this:

 <drink drinkImage="ck.png" drinkTitle="PEPSI" drinkDescription="NONE" /> 

Your actual problem was saving. XDoucment.Save accepts a stream, text editor, xml-writer, or string. In this example, I used the string path. More details here: https://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument(v=vs.110).aspx

EDIT: try using stream:

 var xmlFile = await StorageFile.GetFileFromPathAsync("fav.xml"); using (var fileStream = await xmlFile.OpenStreamForWriteAsync()) { xdoc.Save(fileStream); } 
+2
source share

All Articles