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 ..**
}
}
}
}
I already have an xml file as mentioned above:
From the Project menu, select Add New Item. Select XML File from the Templates pane. Enter the filename in the Name field and press Add. The XML file is added to the project and opens in the XML editor.
Right-click the XML file and select "Open With." This will display a list of programs to open the file in. Select "Notepad" (Windows) or "TextEdit" (Mac). These are the pre-installed text editors for each operating system and should already be on the list. Any basic text editors will work.
XML Notepad is an open-source XML editor written by Chris Lovett and published by Microsoft. The editor features incremental search in both tree and text views, drag/drop support, IntelliSense, find/replace with regular expressions and XPath expressions, and support for XInclude.
This is possibly the correct way (didn't test in windows store project). Pay attention to which element added to which parent element and what inner text being assigned to which element, that is where your original codes incorrectly do (I appreciate the effort though) :
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 online demo (console based apps) : 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>
You can't save your XML document here. All files from app package are read-only. You have to save your xml document into 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());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With