I am making an app in windows store and having a problem in writing xml in created xml file. I have followed this Editing XML file in Windows Store App but it didn't work for me.
I want this xml to be wrote in my xml file on button click.
Any alternate way for this stuff..
<drink>
<drinkImage>ck.png</drinkImage>
<drinkTitle>COKE</drinkTitle>
<drinkDescription>(1793-1844)</drinkDescription>
</drink>
my current file is this:
<?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 have 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 ..**
}
}
}
}
Wrote this in a console app, but it should still work for Windows Store App.
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' rather than 'XElement'. This isn't a problem but the output would have looked like this...
<drink drinkImage="ck.png" drinkTitle="PEPSI" drinkDescription="NONE" />
Your actual problem was saving. XDoucment.Save accepts a stream, text writer, xml writer, or string. In this example I used the string path. See more here: https://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument(v=vs.110).aspx
EDIT: Try using a filestream:
var xmlFile = await StorageFile.GetFileFromPathAsync("fav.xml");
using (var fileStream = await xmlFile.OpenStreamForWriteAsync())
{
xdoc.Save(fileStream);
}