通过代码添加xml标签
我正在用 C# 编写一个应用程序。我已经有一个不为空的 .xml 文件,我想向其中添加新值,而不删除现有值。
我尝试过这段代码:
第一:
FileStream docNewUser = new FileStream(@"C:\\MyApp\\MySubDir\\Data\\" + pr + ".xml", FileMode.Open);
XmlTextWriter xmlNewUser = new XmlTextWriter(docNewUser, null);
xmlNewUser.WriteStartDocument();
xmlNewUser.WriteStartElement("RootEl");//root
xmlNewUser.WriteStartElement("Zapis");
xmlNewUser.WriteStartElement("Name");
xmlNewUser.WriteString(txtEnterName.Text);
xmlNewUser.WriteEndElement();
xmlNewUser.WriteEndElement();//end of zapis
this.Close();
第二:
FileStream docNewUser = new FileStream(@"C:\\MyApp\\MySubDir\\Data\\" + pr + ".xml", FileMode.Open);
XmlTextWriter xmlNewUser = new XmlTextWriter(docNewUser, null);
xmlNewUser.WriteStartDocument();
xmlNewUser.WriteStartElement("RootEl");//root-ot
xmlNewUser.WriteStartElement("Zapis");
xmlNewUser.WriteStartElement("Name");
xmlNewUser.WriteString(txtEnterName.Text);
xmlNewUser.WriteEndElement();
xmlNewUser.WriteEndElement();//end of zapis
xmlNewUser.WriteElementString("Ime", null, txtEnterName.Text);
this.Close();
第三:
FileStream docNewUser = new FileStream(@"C:\\MyApp\\MySubDir\\Data\\" + pr + ".xml", FileMode.Open);
XmlTextWriter xmlNewUser = new XmlTextWriter(docNewUser, null);
xmlNewUser.WriteStartDocument();
xmlNewUser.WriteStartElement("Zapis");
xmlNewUser.WriteStartElement("Name");
xmlNewUser.WriteString(txtEnterName.Text);
xmlNewUser.WriteEndElement();
xmlNewUser.WriteEndElement();//end of zapis
xmlNewUser.WriteElementString("Ime", null, txtEnterName.Text);
this.Close();
我认为问题是流不知道在哪里放置新值。 更多信息:根元素已输入。
I'm writing an application in C#. I already an .xml file that is not empty and I want to add new values to it, without deleting the existing values.
I have tried this code:
First:
FileStream docNewUser = new FileStream(@"C:\\MyApp\\MySubDir\\Data\\" + pr + ".xml", FileMode.Open);
XmlTextWriter xmlNewUser = new XmlTextWriter(docNewUser, null);
xmlNewUser.WriteStartDocument();
xmlNewUser.WriteStartElement("RootEl");//root
xmlNewUser.WriteStartElement("Zapis");
xmlNewUser.WriteStartElement("Name");
xmlNewUser.WriteString(txtEnterName.Text);
xmlNewUser.WriteEndElement();
xmlNewUser.WriteEndElement();//end of zapis
this.Close();
Second:
FileStream docNewUser = new FileStream(@"C:\\MyApp\\MySubDir\\Data\\" + pr + ".xml", FileMode.Open);
XmlTextWriter xmlNewUser = new XmlTextWriter(docNewUser, null);
xmlNewUser.WriteStartDocument();
xmlNewUser.WriteStartElement("RootEl");//root-ot
xmlNewUser.WriteStartElement("Zapis");
xmlNewUser.WriteStartElement("Name");
xmlNewUser.WriteString(txtEnterName.Text);
xmlNewUser.WriteEndElement();
xmlNewUser.WriteEndElement();//end of zapis
xmlNewUser.WriteElementString("Ime", null, txtEnterName.Text);
this.Close();
Third:
FileStream docNewUser = new FileStream(@"C:\\MyApp\\MySubDir\\Data\\" + pr + ".xml", FileMode.Open);
XmlTextWriter xmlNewUser = new XmlTextWriter(docNewUser, null);
xmlNewUser.WriteStartDocument();
xmlNewUser.WriteStartElement("Zapis");
xmlNewUser.WriteStartElement("Name");
xmlNewUser.WriteString(txtEnterName.Text);
xmlNewUser.WriteEndElement();
xmlNewUser.WriteEndElement();//end of zapis
xmlNewUser.WriteElementString("Ime", null, txtEnterName.Text);
this.Close();
I think the problem is that the stream doesn’t know where to put the new value.
some more information: the root element is already entered.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的 .NET 版本支持,请使用 LINQ to XML。 (警告:我不是专家,可能有一种更优雅的方式来编写它。)
我的测试文件(之前):
我的测试文件副本(之后)(我复制而不是替换):
If your .NET version supports it, use LINQ to XML. (Caveat: I'm not an expert, and there's probably a more elegant way to write this.)
My test file (before):
My test file copy (after) (I copied rather than replacing):
当您以这种方式将 XML 写入文件时,它总是会覆盖文件中之前的内容。如果您想使用
XmlTextWriter
对其进行写入,则必须先复制当前内容,然后将新元素写入正确的位置。不要忘记,您无法同时读取和写入同一个文件,因此您必须使用临时文件并用它覆盖原始文件。或者您先将整个文件读入字符串中。或者先将结果写入字符串。但更好的解决方案可能是使用
XDocument
(或XmlDocument
)加载整个文档,修改它,然后保存它。 (如果 XML 文件很大,那么这样做不是一个好主意。)When you write XML to a file this way, it always overwrites what was in the file before. If you want to write to it using
XmlTextWriter
, you would have to copy the current content first, and write the new elements at the proper position. Don't forget that you can't read from and write to the same file at the same time, so you have to use a temporary file and overwrite the original with it. Or you read the whole file into a string first. Or write your result into a string first.But a better solution might be to use
XDocument
(orXmlDocument
) to load the whole document, modify it and then save it. (Doing this is not a good idea if the XML file is huge.)使用 XmlDocument:
我执行此操作时内存不足,因此方法的名称是近似的。
正如另一个答案中所述,使用
XmlElement
加载大型 XML 文件可能成本高昂,因为当您调用Load()
时,它已完全加载到内存中。Use XmlDocument:
I'm doing this out of memory, so the names of the methods are aproximate.
As said in another answer, loading big XML files using
XmlElement
can be costy, because it is fully loaded in memory when you callLoad()
.您使用 XmlTextWriter 有什么原因吗?我发现 LINQ to XML 替代方案要容易得多。
你的代码如下所示;
Is there a reason you are using XmlTextWriter? I find the LINQ to XML alternatives much easier.
your code would be something like below;