locked
XML creating and writing on VBnet RRS feed

  • Question

  • User-1745705970 posted

    Hi,

    I have used following code to create and write the user input data to XML through Dataset. Hope it can be understood by looking the following code:

    Public Class Form3
    
        Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    
            ' Create a DataSet with one table and three columns.
            Dim OriginalDataSet As New DataSet("myDataSet")
            OriginalDataSet.Namespace = "NetFrameWork"
            Dim myTable As New DataTable("myTable")
            Dim c1 As New DataColumn("Acc Number", Type.GetType("System.Int32"))
            'c1.AutoIncrement = True
            Dim c2 As New DataColumn("Name", Type.GetType("System.String"))
            Dim c3 As New DataColumn("Balance $", Type.GetType("System.Double"))
    
    
            myTable.Columns.Add(c1)
            myTable.Columns.Add(c2)
            myTable.Columns.Add(c3)
    
            OriginalDataSet.Tables.Add(myTable)
            Try
                Dim newRow As DataRow
                newRow = myTable.NewRow()
                newRow("Acc Number") = txtAccNumber.Text.ToString()
                newRow("Name") = txtName.Text.ToString()
                newRow("Balance $") = txtBalance.Text.ToString()
                OriginalDataSet.AcceptChanges()
    
                ' Write the schema and data to an XML file.
                Dim xmlFilename As String = "myXmlDocument.xml"
                ' Use WriteXml to write the document.
                OriginalDataSet.WriteXml(xmlFilename)
    
    
            Catch ex As Exception
            End Try
            ' Dispose of the original DataSet.
            OriginalDataSet.Dispose()
    
        End Sub

    But the above code is not working. it is not writing the value in XML file.


    Monday, May 10, 2010 3:47 AM

Answers

  • User-1745705970 posted

    Hi Chintan Dave,


    ok, I found my mistake on the above code. I forget to add:

    myTable.Rows.Add(newRow)

    before the Acceptchanges() function.

    Now it is working Properly.

    Thank u very much.




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 11, 2010 1:57 AM
  • User-2080168305 posted

    i want to give a suggestion. pls take it in positive way.

    whenever u get any error/bug in ur code, just debug it.

    so by debugging u can solve ur problem ur self.

    debugging is good way than just add post in forum.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 11, 2010 2:31 AM

All replies

  • User-2080168305 posted

    well i m giving u a sample code for inserting a record in xml datafile.

    pls refer this.

    Code-behind function(Bussiness logic function):

    public long Insert(string CountryName, string RegionName, string Path, string Language)
            {
                try
                {
                    DataRow dr = default(DataRow);
                    DataSet ds = new DataSet();
    
                    ds.ReadXml(Path + "/Country/" + Language + "/Countries.xml");
                    dr = ds.Tables[0].NewRow();
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        dr["CountryID"] = (long)ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1]["CountryID"] + 1;
                    }
                    else
                    {
                        dr["CountryID"] = 1;
                    }
    
                    dr["CountryName"] = CountryName;
                    dr["RegionName"] = RegionName;
    
                    ds.Tables[0].Rows.Add(dr);
                    ds.WriteXml(Path + "/Country/" + Language + "/Countries.xml", XmlWriteMode.WriteSchema);
                    return 1;
                }
                catch (Exception ex)
                {
                    return 0;
                }
            }


    XML File (Country.xml):

    <?xml version="1.0" standalone="yes"?>
    <Countries>
      <xs:schema id="Countries" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
        <xs:element name="Countries" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
          <xs:complexType>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
              <xs:element name="Country">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="CountryID" type="xs:long" minOccurs="0" />
                    <xs:element name="CountryName" type="xs:string" minOccurs="0" />
                    <xs:element name="RegionName" type="xs:string" minOccurs="0" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:choice>
          </xs:complexType>
        </xs:element>
      </xs:schema>
      <Country>
        <CountryID>1</CountryID>
        <CountryName>India</CountryName>
        <RegionName>Asia</RegionName>
      </Country>
      <Country>
        <CountryID>2</CountryID>
        <CountryName>USA</CountryName>
        <RegionName>America</RegionName>
      </Country>
    </Countries>
    
    
    


    Calling insert function from Manage Country page:


    protected void btnAdd_Click(object sender, EventArgs e)
            {
                    Int64 Result;
                    Result = ObjXMLData.Insert(txtcountryname.Text.Trim(), ddlRegion.SelectedValue, Server.MapPath("~/App_Data"), "English");
                    Result = ObjXMLData.Insert(txtCountrynameSpanish.Text.Trim(), ddlRegion.SelectedValue, Server.MapPath("~/App_Data"), "Spanish");
    
                    if (Result >= 1)
                    {
                        BindGrid();
                        pnladdedit.Visible = false;
                        pnlview.Visible = true;
                    }
                    else
                    {
                        lblerror.Text = "Please enter proper data";
                    }
                    pnladdedit.Visible = false;
                    pnlview.Visible = true;
    }
    
    
    




    Monday, May 10, 2010 6:51 AM
  • User-1745705970 posted

    i think your way is good. But pls can anyone find what is wrong in my above code.. I want to find the  mistakes in my code

    or anything i have to add in the code.


    Pls help Urgent.

    Monday, May 10, 2010 7:53 AM
  • User-2080168305 posted

    # ' Write the schema and data to an XML file.  
    #             Dim xmlFilename As String = "myXmlDocument.xml"  
    #             ' Use WriteXml to write the document.  
    #             OriginalDataSet.WriteXml(xmlFilename)  


    why u dont given full path here in xmlFileName?

    as i give and ur XML File already created or u r creating runtime?

    if it is already created than what is the schema of xml file?


    Monday, May 10, 2010 9:41 AM
  • User-1745705970 posted

    hi,


    Yes i have given the full path of the XML file. WriteXML function writes the both shcema and dataset into the XML file.

    but still the code is not working for inserting the data inside the XML file.


    Tuesday, May 11, 2010 1:37 AM
  • User-2080168305 posted

    can pls compare the schema with writing schema.

    both must be same than only we can write in xml.

    or u can post xml schema here.


    Tuesday, May 11, 2010 1:48 AM
  • User-2080168305 posted

    here u defined coloum as interger and u giving record in row as string.

    Dim c1 As New DataColumn("Acc Number", Type.GetType("System.Int32"))    <--- Integer

    newRow("Acc Number") = txtAccNumber.Text.ToString()  <--  string. [convert string to interget and try]


    same here also u given datatype of coloum double and u entering string record

    Dim c3 As New DataColumn("Balance $", Type.GetType("System.Double")) <--- Double
    newRow("Balance $") = txtBalance.Text.ToString()   <--  string. [convert string to Double and try]


    let me know if any.

    Tuesday, May 11, 2010 1:54 AM
  • User-1745705970 posted

    Hi Chintan Dave,


    ok, I found my mistake on the above code. I forget to add:

    myTable.Rows.Add(newRow)

    before the Acceptchanges() function.

    Now it is working Properly.

    Thank u very much.




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 11, 2010 1:57 AM
  • User-2080168305 posted

    i want to give a suggestion. pls take it in positive way.

    whenever u get any error/bug in ur code, just debug it.

    so by debugging u can solve ur problem ur self.

    debugging is good way than just add post in forum.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 11, 2010 2:31 AM