Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit XML file in Windows Store app

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:

like image 320
mohammad haris Avatar asked Dec 06 '15 14:12

mohammad haris


People also ask

How do I edit an XML file in Windows?

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.

How do I edit an XML file in Windows 10?

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.

What is Microsoft XML editor?

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.


2 Answers

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>
like image 74
har07 Avatar answered Oct 27 '22 03:10

har07


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());
        }
    }
like image 3
t.ouvre Avatar answered Oct 27 '22 02:10

t.ouvre