c# - 使用 XDocument 写入 XML,但知道在何处写入

标签 c# xml winforms linq linq-to-xml

希望你能帮我一点忙。我正在尝试写入 XML 文件,但正在努力编写写入 XML 文件的方法。这是手动编写的 XML 文件(使用 Notepad++ 等):

<software>
    <software_entry
    name="Adobe Acrobat X Standard"
    path="Applications\Acrobat\Acrobat X Standard\AcroStan.msi" 
    type="msi"
    switches="/qn ALLUSERS=1"
    />

    <software_entry
    name="Adobe Acrobat X Professional"
    path="Applications\Acrobat\Acrobat X Pro\AcroPro.msi"
    type="msi"
    switches="/qn ALLUSERS=1"
    />
</software>

这部分应用程序的目的是使用 GUI 编写。

在应用程序中,用户选择 XML 文件的名称。然后将其保存在临时文件夹中,直到在进一步处理过程中询问用户他们希望将其保存在何处。输入所需的文件名称并单击“创建”后,将运行名为“createAndLoadXML”的方法。顾名思义,它创建并加载一个 XML 文件(以填充表单上的 ListView 控件)。代码如下所示。

private void createAndLoadXML()
{
    // Method to create XML file based on name entered by user
    string tempPath = Path.GetTempPath();
    string configFileName = fileNameTextBox.Text;
    string configPath = tempPath + configFileName + ".xml";
    // Create XDocument
    XDocument document = new XDocument(
        new XDeclaration("1.0", "utf8", "yes"),
        new XComment("This XML file defines the software selections for use with the Software Installer"),
        new XComment("XML file generated by Software Installer"),
        new XElement("software",
            new XElement("software_entry",
                new XAttribute("name", ""),
                new XAttribute("path", ""),
                new XAttribute("type", ""),
                new XAttribute("switches", ""))
                )
        );
    document.Save(configPath);
    configCreateLabel.Visible = true;
    document = XDocument.Load(configPath);
}

现在,此表单的下方是 4 个用于用户输入的文本框,每个文本框都与创建的属性(名称、路径、类型和开关)相关然后程序会将这 4 个字段作为属性写入此 XML 文件。到目前为止,我有这段代码,它非常不完整,甚至没有使用 LINQ to XML。

private void writeToXML()
{
    // Method to write lines to XML file based on user input
    // Sets string variables
    string fileName = softwareNameTextBox.Text;
    string filePath = filePathTextBox.Text;
    string fileType = installerType.Text.ToString();
    string installSwitches = installSwitchesTextBox.Text;
    using (XmlWriter xw = XmlWriter.Load(configPath)) //This line is wrong, I know
    {
        xw.WriteStartElement("software");
        xw.WriteElementString("name", fileName);
        xw.WriteElementString("path", filePath);
        xw.WriteElementString("type", fileType);
        xw.WriteElementString("switches", installSwitches);
        xw.WriteEndElement();
    }
}

基本上,任何人都可以帮助我使用上述方法将用户输入文本框控件的数据写入 XML 吗?我不确定如何加载以前创建的 XML 文档(通过我的 createAndLoadXML 方法),以及如何使用 LINQ to XML 在根元素(软件)中写入。

最佳答案

试试这个。我认为这应该让你得到你想要的东西,假设 XML 预先存在,因为你在这个方法之前调用 createAndLoadXML 。我是用 NotePad++ 写的,所以可能会有一两个错误。

private void writeToXML()
{
    // Method to write lines to XML file based on user input
    // Sets string variables
    string fileName = softwareNameTextBox.Text;
    string filePath = filePathTextBox.Text;
    string fileType = installerType.Text.ToString();
    string installSwitches = installSwitchesTextBox.Text;

    string FILE_PATH = "bla.xml";

    XDocument xDoc = XDocument.Load(FILE_PATH);

    xDoc.Root.Add(new XElement("software_entry",
                    new XAttribute("name", fileName),
                    new XAttribute("path", filePath),
                    new XAttribute("type", fileType),
                    new XAttribute("switches", installSwitches)
                ));
    xDoc.Save(FILE_PATH);
}

关于c# - 使用 XDocument 写入 XML,但知道在何处写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14947590/

相关文章:

c# - 如何反序列化仅包含值的 JSON 数组?

json - 如何将 XML 转换为 JSON AWS API Gateway?

java - 在 Java 中以编程方式从 XML 生成 XSD

c# - Winforms 我应该多线程还是使用事件计时器?

c# - 在 Windows 窗体用户控件中嵌入 XNA 游戏

c# - 无法使用与其底层 RCW 分离的 COM 对象

c# - 将参数传递给泛型类类型的构造函数

c# - Elasticsearch Nest - 具有多个过滤器的简单查询

c# - Newtonsoft JSON ShouldSerialize 和继承

xml - 将许多文件从 XML 'File Id' 值批量重命名为 'Short Name' 值