Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to write the data from a web form into XMLfile. I am using the code:
VB
Private Sub test()
        Try
            Dim sFilename As String = Server.MapPath(ConfigurationManager.AppSettings("Path").ToString())
            
           
           

            Dim settings As XmlWriterSettings = New XmlWriterSettings()

            settings.Indent = True

           

            Using writer As XmlWriter = XmlWriter.Create(sFilename, settings)

                writer.WriteStartDocument()
                writer.WriteStartElement("NewDataSet")

                writer.WriteStartElement("dtCare")
                writer.WriteElementString("ID", "11111111111")
                writer.WriteElementString("REASON", txtReason.Text)
                writer.WriteElementString("CVFLAG", RadioButtonList1.SelectedItem.Text)
                writer.WriteElementString("NATURE", txtReasonCode.SelectedValue)
                writer.WriteElementString("DETAILS", txtDetails.Text)
                writer.WriteElementString("CDATE", Format(Date.Now, "yyyyMMdd"))
                writer.WriteElementString("CTIME", Format(Date.Now, "HHmmss"))
                writer.WriteEndElement()



                writer.WriteEndElement()
                writer.WriteEndDocument()

            End Using


            Response.Write("Successfull")
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub


Its working fine for the first time creating XML file like:

XML
<?xml version="1.0" encoding="utf-8"?>
<NewDataSet>
  <dtCare>
    <ID>11111111111</ID>
    <REASON>rsn</REASON>
    <CVFLAG>Yes</CVFLAG>
    <NATURE>IL</NATURE>
    <DETAILS>deatilseh</DETAILS>
    <CDATE>20140228</CDATE>
    <CTIME>132914</CTIME>
  </dtCare>
</NewDataSet>




Its working fine for the first time, but again when i try to add another node, its not working. It just replaces the existing node to the new one.

Please suggest.
Posted

1 solution

1.Create DataTable with Required columns in a xml
C#
DataTable dt = new DataTable();
       dt.Columns.Add("Name");

2.Add rows to Datatable
C#
DataRow dr = dt.NewRow();
          dr["Name"] = "XYZ";
          dt.Rows.Add(dr);

4.Add that datatable to Dataset
C#
DataSet ds = new DataSet();
            ds.Tables.Add(dt);

5.Directly use Ds write.
C#
ds.WriteXml("d:\\1.xml");
 
Share this answer
 
Comments
ujju.1 28-Feb-14 4:12am    
thanx mate...it worked just fine :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900