-1
這是我追加新記錄,以我現有的XML文檔代碼:將數據添加到現有的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
但執行這段代碼當過,新的數據的文件條目將覆蓋現有的那些。
基本上,我想輸出是這樣的,例如(觀察在瀏覽器的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>
但是,這是我所得到的:
<BIO_INFO>
<CLIENT>
<NAME>Elena</NAME>
<AGE>21</AGE>
<SEX>FEMALE</SEX>
</CLIENT>
</BIO_INFO>"
這裏有我的聲明:
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")
你可以試試這個:http://stackoverflow.com/questions/13237514/appending-information-to-an-xml-file-in-vb-net?rq = 1 或http://stackoverflow.com /問題/ 2645440 /附加-AN-現有的XML文件?RQ = 1 – Nianios