Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Appending Data to xml file

Posted on 2007-03-30
2
Medium Priority
?
212 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 750 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

721 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