首页 > 解决方案 > 将字符串写入位于 appdata\local 中的 xml 文件

问题描述

我是 C# 新手,我想知道如何<Package name="gaffersimulations-fale" active="true"/>在位于 appdata/local 的 xml 文件中的特定标签之间写入字符串。

我想要达到的目标:

<content>
  <Package name="gaffersimulations-fale" active="true"/>
</content>

标签: c#xml

解决方案


使用 xml linq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            XElement content = new XElement("content");

            XElement package = new XElement("Package", new object[] {
                new XAttribute("name", "gaffersimulations-fale"),
                new XAttribute("active", "true")
            });
            content.Add(package);
        }
    }
}

推荐阅读