Solved

Appending Data to xml file

Posted on 2007-03-30
2
204 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:ShaileshShinde
2 Comments
 
LVL 7

Accepted Solution

by:
dctuck earned 250 total points
ID: 18822532
This sample XML file you have shown would not be valid - you have multiple instances of field names within the same level. It looks like you're trying to write seperate records, so you should probably organise it as something like:

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

Then you can do something along these lines:

myDoc = New Xml.XmlDocument()
myDoc.LoadXml("<March><Record><SrNo>1</SrNo><Title>test1</Title><Amount>559012</Amount><Status>Paid</Status></Record></March>")
Dim ParentNode As Xml.XmlNode = myDoc.DocumentElement
Dim newChild As Xml.XmlElement = myDoc.CreateElement("Record")
newChild.InnerXml = "<SrNo></SrNo><Title>Title</Title><Amount></Amount><Status></Status>"
ParentNode.AppendChild(newChild)

but you will obviously need to edit the InnerXml element to include your values....
0
 
LVL 1

Author Comment

by:ShaileshShinde
ID: 18822697
Hello Expert,

Thanks a lot!!!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question