Editing existing Xml file

Timbo292832

Member
Joined
Apr 7, 2013
Messages
5
Location
Nsw Australia
Programming Experience
Beginner
Hi Guys
lately i have been working with xml files
i am able to write a xml file and read it but when i write to the xml file it overwrites everything that is not to be overwritten
it overwrites the file completely with a new xml

can someone please tell me how i can edit the xml
as in add elements without loosing my existing ones

here is a example of my code to writing xml

Dim settings As New XmlWriterSettings()
settings.Indent = True
Dim XmlWrt As XmlWriter = XmlWriter.Create("MyXML.xml", settings)

With XmlWrt

' Write the Xml declaration.
.WriteStartDocument()

' Write a comment.
.WriteComment("XML Database.")

' Write the root element.
.WriteStartElement("Data")

' Start our first person.
.WriteStartElement("Person")

' The person nodes.

.WriteStartElement("FirstName")
.WriteString("Alleo")
.WriteEndElement()

.WriteStartElement("LastName")
.WriteString("Indong")
.WriteEndElement()


' The end of this person.
.WriteEndElement()

' Close the XmlTextWriter.
.WriteEndDocument()
.Close()

End With

MessageBox.Show("XML file saved.")
End If
 
Hi,

I have never used these classes before, but it would make sense to me that, if you need to preserve the contents of an original file you are working with then you would have to use an XMLReader Class first to read the file into your project, then make whatever modifications you need, and then use an XMLWriter Class to write all your information back to a file.

That said, and depending on what you need to do, have you looked at using a DataTable or a DataSet to Read and Write your XML files? By using either of these components, you can read your XML file, bind your data fields to whatever control(s) you want, make as many modifications to your data as you like and then write your complete data back to the XML file? The only difference between using a DataSet and a DataTable to read your XML file is that a DataSet will Infer the structure of your DataTable(s) based on the Structure of your XML file whereas a DataTable will not.

Hope that helps.

Cheers,

Ian
 
Back
Top