C# 使用XDocument写入XML,但知道在哪里写入
希望你能帮我一点忙。我试图写入一个XML文件,但我很难编写写入XML文件的方法。这是手动编写的XML文件(使用记事本++等): 现在,在这个表单的下面是4个文本框,用于用户输入,每个文本框都与创建的属性(名称、路径、类型和开关)相关。用户将在这些文本框中写入内容,单击“添加”按钮,然后程序将这4个字段作为属性写入这个XML文件。到目前为止,我有这段代码,它非常不完整,甚至没有使用LINQtoXMLC# 使用XDocument写入XML,但知道在哪里写入,c#,xml,winforms,linq,linq-to-xml,C#,Xml,Winforms,Linq,Linq To Xml,希望你能帮我一点忙。我试图写入一个XML文件,但我很难编写写入XML文件的方法。这是手动编写的XML文件(使用记事本++等): 现在,在这个表单的下面是4个文本框,用于用户输入,每个文本框都与创建的属性(名称、路径、类型和开关)相关。用户将在这些文本框中写入内容,单击“添加”按钮,然后程序将这4个字段作为属性写入这个XML文件。到目前为止,我有这段代码,它非常不完整,甚至没有使用LINQtoXML private void writeToXML() { // Method to writ
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
。我是用记事本++写的,所以可能有一两个错误
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();
}
}
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作为XDocumnt加载,使用表单中填写的xattributes创建新的Xelement,将其添加到内存中的XDocument,然后将其保存到现有的XML文件上。感谢您的建议。感谢您的回复-尽管我不理解字符串文件的路径。我的情况应该是什么?因为XML文件的文件路径依赖于createAndLoadXML方法中的用户输入(您可以看到变量configPath),所以我可以使用它吗?再次感谢。他说的是,不管文件的正确路径是什么——用它代替“文件路径”。啊,对了。对不起,我昨晚发得很晚,脑子有点模糊。非常感谢贾斯汀和约翰。
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);
}