Results 1 to 6 of 6

Thread: appending data to an XML file

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    18

    appending data to an XML file

    Hi can someone help me with appending data to an XML file.

    I want to save some data to a XML file which is done by the following code.

    VB Code:
    1. Private Sub Command47_Click()
    2. CommonDialog1.ShowSave
    3. NewFileSave = CommonDialog1.FileName
    4. MsgBox NewFileSave
    5. xml.SaveXml NewFileSave
    6. MsgBox "Saved"
    7. CancelButton:
    8. Exit Sub
    9. End Sub

    Now if i open an existing file and save my data in that file the original data is lost.

    How to append the data to the existing XML file ???

    Amit

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    18

    Re: appending data to an XML file

    Urgent help required as i am stuck with this problem.

    I am storing some data in the buffer through input from a GUI and am saving the data using the code mentioned in the 1st post to a new XML file but i am not able to append the same data stored in the buffer to an existing XML file.

    Can anyone help me out ??????

    Thanks,
    Amit

  3. #3
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: appending data to an XML file

    Here is the code to append xml:
    VB Code:
    1. Sub Append_XML_Element(oXMLfile As String)
    2. ' Requires msxml.dll (Go to Project --> References and and choose Microsoft XML version 2.0, or whatever the
    3. ' current version you have installed)
    4.     Dim objDom As New MSXML2.DOMDocument26
    5.     Dim objRootElem As IXMLDOMElement
    6.     Dim objSubRootElem As IXMLDOMElement
    7.     Dim objMemberElem As IXMLDOMElement
    8.  
    9.     'Load xml files
    10.     objDom.async = False
    11.     objDom.Load oXMLfile
    12.  
    13.     ' Loads root element
    14.     Set objRootElem = objDom.documentElement
    15.  
    16.     ' Creates sub root element
    17.     Set objSubRootElem = objDom.createElement("MySubRootElement")
    18.     objRootElem.appendChild objSubRootElem
    19.  
    20.     ' Creates Error Date & Time element
    21.     Set objMemberElem = objDom.createElement("DateTime")
    22.     objSubRootElem.appendChild objMemberElem
    23.     objMemberElem.Text = Format(Now, "DD-MMM-YYYY hh:mm:ss")
    24.  
    25.     ' Saves XML data to disk.
    26.     objDom.save oXMLfile
    27.  
    28. ExitHere:
    29.     Err.Clear
    30.     Set objDom = Nothing
    31. End Sub
    CS

  4. #4
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: appending data to an XML file

    Please modify as per your need.
    CS

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    18

    Re: appending data to an XML file

    Thanks for the hep but what i am going to append is not predefined and is only entered by the user using a gui and i need to save the enteries made by the user to the exisiting XML file

    Amit

  6. #6
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: appending data to an XML file

    No probs! Change the field name and its value in the above code as per your requirement.

    Are you appending the data or going to edit existing data?
    Quote Originally Posted by kamitsin
    Thanks for the hep but what i am going to append is not predefined and is only entered by the user using a gui and i need to save the enteries made by the user to the exisiting XML file
    For any data there will be a constant field name and in run time the user will enter the value for that field right. So, if you have filed "Student Name" and in run time the user is entering the name of the student "Amit" in a text box (Let's say name of the text box txtStudent) then in above code you need to change as:
    VB Code:
    1. ' Creates Student Name element
    2.     Set objMemberElem = objDom.createElement("StudentName")
    3.     objSubRootElem.appendChild objMemberElem
    4.     objMemberElem.Text = txtStudent.Text
    Everytime you need to use this code to append a new node on xml.
    CS

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width