Solved

Appending Data to xml file

Posted on 2007-03-30
2
196 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
Comment Utility
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
Comment Utility
Hello Expert,

Thanks a lot!!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now