C# 如何在c语言中修改xml文件#

C# 如何在c语言中修改xml文件#,c#,xml,uwp,C#,Xml,Uwp,我想使用XML作为用户数据。我想添加一项功能,以便用户可以在我的应用程序中编辑他们的个人资料。但是,我在XML编辑方面遇到了一个问题 using Windows.Data.Xml.Dom; using Windows.Storage; private async void btn_save(object sender, RoutedEventArgs e) { StorageFolder storageFolder = ApplicationData.Current.LocalFol

我想使用XML作为用户数据。我想添加一项功能,以便用户可以在我的应用程序中编辑他们的个人资料。但是,我在XML编辑方面遇到了一个问题

using Windows.Data.Xml.Dom;
using Windows.Storage;

private async void btn_save(object sender, RoutedEventArgs e)
{
     StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
     StorageFile file = await  storageFolder.GetFileAsync("file.xml");
     string fileContent = await FileIO.ReadTextAsync(file);

     XmlDocument doc = new XmlDocument();
     doc.LoadXml(fileContent);
     XmlNodeList tags = doc.GetElementsByTagName("person");

     if (tags.Count > 0)
     {
          int index;
          for (int i = 0; i < tags.Count; i++)
          {
               if(tags.ElementAt(i).Attributes.GetNamedItem("id").InnerText == personId && tags.ElementAt(i).Attributes.GetNamedItem("id") != null)
               {
                   index = i;
                   break;
               }
          }
          XmlAttribute attr_name = doc.CreateAttribute("name");
          attr_name.Value = txtBox_name.Text;
          tags.ElementAt(index).Attributes.SetNamedItem(attr_name);

          await doc.SaveToFileAsync(file);
     }
}
我怀疑这是个大问题。我不知道如何保存修改过的xml。 无论如何,即使在我开始修改XML之前,它也不是空的。那里已经有一些标签了

请在此处查看我的项目副本:

这是因为我的xml文件在保存后变为空。那么,如何修改我的xml文件呢

在测试过程中,问题似乎出现在
doc.SaveToFileAsync(file)
行中,请随时向windows反馈中心报告,如果您已报告,请告诉我,目前,我们有一个解决方法,可以编写字符串替换方法

if(tags.Count>0)
{
int指数=0;
对于(int i=0;i
这是因为我的xml文件在保存后变为空。那么,如何修改我的xml文件呢

在测试过程中,问题似乎出现在
doc.SaveToFileAsync(file)
行中,请随时向windows反馈中心报告,如果您已报告,请告诉我,目前,我们有一个解决方法,可以编写字符串替换方法

if(tags.Count>0)
{
int指数=0;
对于(int i=0;i
在循环中保存文件在我看来并不合理-请在完成循环后只保存一次。@Filburt因为每个人的id都是唯一的,所以只能保存一次。我用它来搜索一个目标标签,这样就不需要循环了。在任何情况下,我都会将它移出循环(在
if
中)并检查返回的ActionResult-我非常确定它将能够告诉您出了什么问题。我没有时间仔细看看你在这里做什么,但我怀疑Xml文档的更改是有原因的。@Filburt好的,我已经移动了它。但它没有解决任何问题,在我看来,问题出现在
doc.LoadXml(fileContent),即在您开始修改文件之前,该文件为空。这就是为什么你应该1。)为你得到的错误发布一个调用堆栈,2。)将你的代码减少到一个时间段,同时尝试删除不影响结果的代码,你可能会发现只需要5行代码,整个循环都不相关。我觉得这不合理,在循环中保存文件-在完成循环后只保存一次。@Filburt因为每个人的id都是唯一的,所以只能保存一次。我用它来搜索一个目标标签,这样就不需要循环了。在任何情况下,我都会将它移出循环(在
if
中)并检查返回的ActionResult-我非常确定它将能够告诉您出了什么问题。我没有时间仔细看看你在这里做什么,但我怀疑Xml文档的更改是有原因的。@Filburt好的,我已经移动了它。但它没有解决任何问题,在我看来,问题出现在
doc.LoadXml(fileContent),即在您开始修改文件之前,该文件为空。这就是为什么你应该1。)为你得到的错误发布一个调用堆栈,2。)在尝试删除不影响结果的代码时,你可能会发现只需要5行代码,整个循环都不相关。这里只是猜测一下,但是如果querent在.NET Core 3.x上运行,那么这可能与.Yep有关,但是
Windows.Data.Xml.Dom
是特定于uwp的,它不适用于.NET Core 3.x。非常感谢,这解决了我的问题。这里只是一个猜测,但是如果querent在.NET Core 3.x上运行,那么这可能与.Yep有关,但是
Windows.Data.Xml.Dom
是特定于uwp的,它不适用于.NET Core 3.x。非常感谢,这解决了我的问题。
await doc.SaveToFileAsync(file);
if (tags.Count > 0)
{
    int index = 0;
    for (int i = 0; i < tags.Count; i++)
    {
        if (tags.ElementAt(i).Attributes.GetNamedItem("id").InnerText == "12345678901234"/*this is a unique id, based on xml*/ && tags.ElementAt(i).Attributes.GetNamedItem("id") != null)
        {
            index = i;
            break;
        }
    }

    XmlAttribute attr_name = doc.CreateAttribute("name");
    attr_name.Value = txtBox_name.Text;
    tags.ElementAt(index).Attributes.SetNamedItem(attr_name);
     // get xml string
    var data = doc.GetXml();
     /* await doc.SaveToFileAsync(file); */
    await FileIO.WriteTextAsync(file, data);

}