-1

This is my code for Appending New Records to my existing XML Document:

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):

<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")
Ry-
  • 218,210
  • 55
  • 464
  • 476
Blitzendegen
  • 9
  • 1
  • 1
  • You can try this :http://stackoverflow.com/questions/13237514/appending-information-to-an-xml-file-in-vb-net?rq=1 or http://stackoverflow.com/questions/2645440/appending-an-existing-xml-file?rq=1 – Nianios May 31 '13 at 14:26

1 Answers1

0

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

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 .Save and .Load out of your method and trigger them separately (like you are processing a text file in a text editor).

Community
  • 1
  • 1
IvanH
  • 5,039
  • 14
  • 60
  • 81