vb.net 将数据附加到现有的 XML 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16859120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Append data to existing XML file
提问by Blitzendegen
This is my code for Appending New Records to my existing XML Document:
这是我将新记录附加到现有 XML 文档的代码:
Sub addEXISTING(ByVal c_name As String, ByVal c_age As Integer, ByVal c_sex As String)
Dim e_client = doc.CreateElement("CLIENT")
Dim e_name As Xml.XmlElement = doc.CreateElement("NAME")
Dim e_age As Xml.XmlElement = doc.CreateElement("AGE")
Dim e_sex As Xml.XmlElement = doc.CreateElement("SEX")
e_name.InnerText = c_name
e_age.InnerText = c_age
e_sex.InnerText = c_sex
e_client.AppendChild(e_name)
e_client.AppendChild(e_age)
e_client.AppendChild(e_sex)
childparent.AppendChild(e_client)
doc.AppendChild(childparent)
doc.Save("D:\mefolder\me.xml")
MsgBox("XML DOCUMENT UPDATED!", MsgBoxStyle.Information, "Notice:")
End Sub
But when ever this piece of code is executed, the file entries of the new data overwrite the existing ones.
但是当这段代码被执行时,新数据的文件条目会覆盖现有的。
Basically, the output I would like is this, for example (when viewing the XML document in a browser):
基本上,我想要的输出是这样的,例如(在浏览器中查看 XML 文档时):
<BIO_INFO>
<CLIENT> ----- EXISTING
<NAME>John</NAME>
<AGE>21</AGE>
<SEX>MALE</SEX>
</CLIENT>
<CLIENT> ----- NEW ENTRY
<NAME>Elena</NAME>
<AGE>21</AGE>
<SEX>FEMALE</SEX>
</CLIENT>
</BIO_INFO>
But this is what I get:
但这就是我得到的:
<BIO_INFO>
<CLIENT>
<NAME>Elena</NAME>
<AGE>21</AGE>
<SEX>FEMALE</SEX>
</CLIENT>
</BIO_INFO>"
Here are my declarations:
以下是我的声明:
Dim filer As DirectoryInfo = New DirectoryInfo("D:\mefolder")
Dim doc As New XmlDocument
Dim root As XmlElement = doc.CreateElement("CLIENT")
Dim childparent As XmlElement = doc.CreateElement("BIO_INFO")
Dim child As XmlElement = doc.CreateElement("NAME")
Dim childage As XmlElement = doc.CreateElement("AGE")
Dim childsex As XmlElement = doc.CreateElement("SEX")
回答by IvanH
It is not possible to append data to xml document. You need to load the whole document and save it again (from nianios comment Appending an existing XML file). It is necessary to use XmlDocument.Load Method
无法将数据附加到 xml 文档。您需要加载整个文档并再次保存(来自 nianios 注释附加现有的 XML 文件)。有必要使用XmlDocument.Load 方法
Sub addEXISTING(ByVal c_name As String, ByVal c_age As Integer, ByVal c_sex As String)
XmlDocument.Load("D:\mefolder\me.xml")'!
Dim e_client = doc.CreateElement("CLIENT")
Dim e_name As Xml.XmlElement = doc.CreateElement("NAME")
Dim e_age As Xml.XmlElement = doc.CreateElement("AGE")
Dim e_sex As Xml.XmlElement = doc.CreateElement("SEX")
e_name.InnerText = c_name
e_age.InnerText = c_age
e_sex.InnerText = c_sex
e_client.AppendChild(e_name)
e_client.AppendChild(e_age)
e_client.AppendChild(e_sex)
childparent.AppendChild(e_client)
doc.AppendChild(childparent)
doc.Save("D:\mefolder\me.xml")
MsgBox("XML DOCUMENT UPDATED!", MsgBoxStyle.Information, "Notice:")
End Sub
Please be aware that this opening and saving for every record is not very efficient especially when the document is large. Consider taking .Saveand .Loadout of your method and trigger them separately (like you are processing a text file in a text editor).
请注意,这种打开和保存每条记录的效率并不高,尤其是当文档很大时。考虑采取.Save与.Load您的方法,并分别触发它们(就像你正在处理在文本编辑器的文本文件)。

