写XML使用的XDocument,但我们知道在哪里写 [英] Writing to XML using XDocument, but knowing where to write

查看:218
本文介绍了写XML使用的XDocument,但我们知道在哪里写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望你能帮助我一下。我试图写一个XML文件,但我在努力写一个,好了,写到XML文件的方法。这是手工编写的XML文件(使用记事本++等):

Hope you can help me a bit. I'm trying to write to an XML file, but am struggling to write the method which, well, writes to the XML file. This is the XML file manually written (using Notepad++ etc.):

<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

The aim of this part of the application is to write that using a GUI.

在该应用程序中,用户选择了XML文件的名称。然后,它被保存在临时文件夹,直到进一步的过程中,当用户问他们想保存它。在进入该文件的所需名称和点击创建,运行称为createAndLoadXML的方法。正如其名字所显示的,它会创建并加载一个XML文件(填充表单上ListView控件)。代码可以看到下面。

In the application, the user chooses the name of the XML file. It is then saved in the temp folder until further in the process when the user is asked where they would like to save it. Upon entering the desired name of the file and clicking Create, the method called "createAndLoadXML" is run. As its name would suggest, it creates and then loads an XML file (to populate a listview control on the form). Code can be seen below.

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到XML。

Now, further down this form are 4 text boxes for user input, each relating to the attributes created (name, path, type and switches) The idea is the user will write in these text boxes, click an 'Add' button and then the program will write those 4 fields as attributes to this XML file. So far, I have this code, which is horribly incomplete and doesn't even use 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的根元素(软件)中编写。

Basically, could anyone please help me with the above method which writes to the XML the data the user has entered into the text box controls? I'm not sure how to load the previously created XML document (from my createAndLoadXML method), and how to write within the root element (software) using LINQ to XML.

推荐答案

尝试了这一点。我想,这应该得到你想要的东西假设XML事先存在,因为你是此方法之前调用 createAndLoadXML 。我在记事本++写了这个,所以我可能有错误或两个。

Try this out. I think this should get you what you want assuming the XML exists beforehand since you are calling createAndLoadXML before this method. I wrote this in NotePad++, so I may have a error or two.

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

这篇关于写XML使用的XDocument,但我们知道在哪里写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆