在Windows Store应用中编辑XML文件

穆罕默德·哈里斯

我正在Windows商店中制作一个应用程序,并且在创建的xml文件中编写xml时遇到问题。我已在Windows Store App中遵循此Editing XML文件,但对我而言不起作用。

我希望在单击按钮时将此xml写入我的xml文件中。

这个东西的任何替代方法..

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

我当前的文件是这样的:

   <?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>

这是我尝试过的:

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 ..**
            }



        }




    }
}

我已经有一个如上所述的xml文件:

特拉维斯船夫

在控制台应用程序中编写了此代码,但它仍适用于Windows Store应用程序。

      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");

输出:

<?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>

您的原始代码非常接近您想要的代码。您使用的是“ XAttribute”而不是“ XElement”。这不是问题,但是输出看起来像这样……

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

您的实际问题是保存。XDoucment.Save接受流,文本编写器,xml编写器或字符串。在此示例中,我使用了字符串路径。在此处查看更多信息:https : //msdn.microsoft.com/zh-cn/library/system.xml.linq.xdocument(v=vs.110).aspx

编辑:尝试使用文件流:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Windows Store应用程序对于XML文件中的每个项目

Windows Store应用中的ReadTextAsync与多字节文件和JavaScript

如何在Powershell中编辑Windows应用商店设置的settings.dat文件?

为什么C ++ Windows Store应用程序头文件对cpp文件中的包含感到满意?

在 Windows 中编辑受 UAC 保护的文件

Windows Store应用中的长期开发实施

如何从 WSL 打开 Windows 文件编辑应用程序

将Windows Store应用中的画布另存为图像文件

如何在8.1的Windows Store应用程序中以编程方式从URI启动PDF文件?

什么是Windows中的.DS_Store文件

即使拥有所有权,在编辑Windows Store应用程序文件时也会出现“访问被拒绝”

使用powershell编辑Windows批处理文件中的文件

Windows Store应用程序与Windows Store Phone应用程序中的StatusBar等效是什么?

在 Windows 中编辑 grub

从Assets Windows Store应用程序获取所有文件

从Windows Store应用访问本地文件夹?

在Windows 8应用中创建PDF文件

如何编辑Windows .url文件

grep -f在Windows中编辑的文件上失败

Windows 8.1上的XAMPP-无法在htdocs中编辑文件

如何将从Windows Store下载的应用程序的快捷方式放置到文件夹中

限制Windows Store应用中的图片大小

在Windows Store应用中的履历上启动相机

Windows Store应用程序中的自定义MessageDialougeBox

在Windows Store中获取应用内购买的价格

如何在AutoHotKey中启动Windows Store应用程序?

如何查看Windows Store应用程序中的更改?

在Windows 10中还原Microsoft Store应用程序

在Windows Store应用中实施长时间运行的过程