Solved

Appending Data to xml file

Posted on 2007-03-30
2
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp.net figure out error 3 45
make control visible based off other control value 7 44
VB.Net Data Class 1 44
Replace statements HTML with HTML IF 8 61
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

737 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