Sunday, November 6, 2011

operation with xml

download XML Fundamental

XMLFILE.xml
.........................
<?xml version="1.0" encoding="utf-8" ?>
<countries>

  <country>
    <text>Norway</text>
    <value>N</value>
  </country>

  <country>
    <text>Sweden</text>
    <value>S</value>
  </country>

  <country>
    <text>France</text>
    <value>F</value>
  </country>

  <country>
    <text>Italy</text>
    <value>I</value>
  </country>

</countries>
--------------------------------
Read data from  xml file
@@@@@@@@@@@@@@@@



 ds.ReadXml(MapPath("XMLFile.xml"));

        DropDownList1.DataSource = ds;
        DropDownList1.DataTextField = "text";
        DropDownList1.DataValueField = "value";
        DropDownList1.DataBind();

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Write XML
---------------------
  protected void btnUpdate_Click(object sender, EventArgs e)
    {
        ds.ReadXml(MapPath("XMLFile.xml"));
        ds.Tables["country"].Rows[0]["text"] = txtName.Text;
              ds.WriteXml(MapPath("XMLFile.xml"));
             ds.Clear();
        bindD();

    }


 private void bindD()
    {
        ds.ReadXml(MapPath("XMLFile.xml"));
        //  Label1.Text=  ds.Tables[0].Rows[0]["text"].ToString();
        DropDownList1.DataSource = ds;
        DropDownList1.DataTextField = "text";
        DropDownList1.DataValueField = "value";
        DropDownList1.DataBind();
    }

Add A New Record..
--------------------


protected void btnAdd_Click(object sender, EventArgs e)
    {
        ds.ReadXml(MapPath("XMLFile.xml"));
        DataTable dt = null;
        DataRow dr = null;
        dt = ds.Tables[0];
        dr = dt.NewRow();
        dr["text"] = txtName.Text;
        dt.Rows.Add(dr);
       
       
        ds.WriteXml(Server.MapPath("XMLFile.xml"));
        // Save it to a new file
   

       
        ds.Clear();
        bindD();
    }

DELETE OPERATION
-----------------------


 protected void btnDelete_Click(object sender, EventArgs e)
    {
        ds.ReadXml(MapPath("XMLFile.xml"));
        DataRow dr1 = ds.Tables[0].Rows[0];
        dr1.Delete();
        ds.WriteXml(Server.MapPath("XMLFile.xml"));
        ds.Clear();
        bindD();
    }



No comments:

Post a Comment