Link to home
Start Free TrialLog in
Avatar of ShaileshShinde
ShaileshShindeFlag for India

asked on

Appending Data to xml file

Hello Experts,

I am attempting to append a new data to an xml file, where as my xml file contains root element "$current month" and when user add details at the first get saved into xml file as...

<?xml version="1.0" encoding="utf-8"?>
<March>
  <SrNo>1</SrNo>
  <Title>test1</Title>
  <Amount>559012</Amount>
  <Status>Paid</Status>
</March>

With below coding as ....

Public Class Expense_Details
    Private Sub Expense_Details_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ComboBox1.Items.Add("Paid")
        ComboBox1.Items.Add("UnPaid")
        'Display current month
        Dim currentmonth As String
        currentmonth = String.Format("{0:MMMM}", DateTime.Now).ToString()
        Label3.Text = currentmonth
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim srNo As Integer = TextBox1.Text
        Dim Title As String = TextBox2.Text
        Dim Amt As Integer = TextBox3.Text
        Dim Status As String = ComboBox1.SelectedItem
        Dim mywriter As XmlWriter = XmlWriter.Create(Application.StartupPath & "\XmlTest.xml")
        mywriter.WriteStartDocument()
        mywriter.WriteStartElement(String.Format("{0:MMMM}", DateTime.Now).ToString())
        mywriter.WriteEndElement()
        mywriter.Flush()
        mywriter.Close()
        Dim xmldoc As XmlDocument = New XmlDocument
        xmldoc.Load(Application.StartupPath & "\XmlTest.xml")
        Dim root As XmlElement = xmldoc.SelectSingleNode(String.Format("{0:MMMM}", DateTime.Now).ToString())
        Dim element1 As XmlElement = xmldoc.CreateElement("SrNo")
        Dim value As XmlText = xmldoc.CreateTextNode(srNo)
        element1.AppendChild(value)
        root.AppendChild(element1)
        Dim element2 As XmlElement = xmldoc.CreateElement("Title")
        Dim value2 As XmlText = xmldoc.CreateTextNode(Title)
        element2.AppendChild(value2)
        root.AppendChild(element2)
        Dim element3 As XmlElement = xmldoc.CreateElement("Amount")
        Dim value3 As XmlText = xmldoc.CreateTextNode(Amt)
        element3.AppendChild(value3)
        root.AppendChild(element3)
        Dim element4 As XmlElement = xmldoc.CreateElement("Status")
        Dim value4 As XmlText = xmldoc.CreateTextNode(Status)
        element4.AppendChild(value4)
        root.AppendChild(element4)
        xmldoc.Save(Application.StartupPath & "\XmlTest.xml")
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Application.Exit()
    End Sub
End Class

Can you please help me with an button next click, the user enters data which should get added to existing data like...

<?xml version="1.0" encoding="utf-8"?>
<March>
  <SrNo>1</SrNo>
  <Title>test1</Title>
  <Amount>559012</Amount>
  <Status>Paid</Status>
  <SrNo>2</SrNo>
  <Title>useradded</Title>
  <Amount>5590</Amount>
  <Status>UnPaid</Status>
</March>

Is there any references or examples for the same.

Thanks,
Shailesh
ASKER CERTIFIED SOLUTION
Avatar of dctuck
dctuck
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ShaileshShinde

ASKER

Hello Expert,

Thanks a lot!!!