Archive

Archive for the ‘XML’ Category

Bind DropdownList from XML file

October 27, 2009 1 comment

Hi

try this example to Bind DropdownList from xml file on server

1) Open VS 2005 and create new website

2) Add new xml file “XMLFile.xml”

<?xml version="1.0" encoding="utf-8" ?>
<Countries>
  <Country>
    <ID>1</ID>
    <Name>Nepal</Name>
  </Country>
  <Country>
    <ID>2</ID>
    <Name>India</Name>
    <Country>
      <ID>3</ID>
      <Name>China</Name>
    </Country>
    <Country>
      <ID>4</ID>
      <Name>Bhutan</Name>
    </Country>
    <Country>
      <ID>5</ID>
      <Name>USA</Name>
    </Country>
  </Country>
</Countries>

3) Add new Web page and inside it add DropdownList control

4) In Code behind add the following code:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    public void Bind()
    {
        DataSet ds = new DataSet(); 
        ds.ReadXml(Server.MapPath("~/XMLFile.xml")); 
 
        //get the dataview of table "Country", which is default table name 
        DataView dv = ds.Tables[0].DefaultView; 

        //Now sort the DataView vy column name "Name" 
        dv.Sort = "Name"; 
        //now define datatext field and datavalue field of dropdownlist 
        DropDownList1.DataTextField = "Name";
        DropDownList1.DataValueField = "ID"; 
 
        //now bind the dropdownlist to the dataview 
        DropDownList1.DataSource = dv;
        DropDownList1.DataBind(); 
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }
}

Hope this helps

Good Luck

Categories: ASP.Net, XML

Convert database tables into XML and Schema

October 27, 2009 Leave a comment

Hi

try this example :-

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class xml : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string strConnect = ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
        string strSelect = "SELECT * FROM Employees";
        DataSet objDataSet = new DataSet();
        SqlConnection objConnect = new SqlConnection(strConnect);
        SqlDataAdapter objDataAdapter = new SqlDataAdapter(strSelect, objConnect);
        objDataAdapter.Fill(objDataSet, "Employees");
        string strVirtualPath = Request.ApplicationPath + "/Employees.xml";
        string strVSchemaPath = Request.ApplicationPath + "/Employees.xsd";
        objDataSet.WriteXml(Request.MapPath(strVirtualPath));
        objDataSet.WriteXmlSchema(Request.MapPath(strVSchemaPath));
    }
}

Hope this helps

Good Luck

Categories: XML

LINQ to XML to Add and Read Data from XML File

August 15, 2009 Leave a comment

Hi

try this example to use LINQ to XML to Add  and Read Data from XML File

1) Open VS 2008 and create new website

2) Add new XML file  called :- Pepole.xml

<?xml version="1.0" encoding="utf-8"?>
<Persons>
  <Person>
    <Name>Yasser</Name>
    <City>Cairo</City>
    <Age>25</Age>
  </Person>
</Persons>

3) Add new web page :-

<div>
    <strong>Add to XML</strong><br />
 Name:<br />
 <asp:TextBox ID="txtName" runat="server" /><br />
 City:<br />
 <asp:TextBox ID="txtCity" runat="server" /><br />
 Age:<br />
 <asp:TextBox ID="txtAge" runat="server" /><br />
 <asp:Button ID="butAdd" runat="server" Text="Add" onclick="butAdd_Click" /><br />
 <asp:Label ID="lblStatus" runat="server" />
 <br /><br />
 <strong>Read XML:</strong><br />
 <asp:Button ID="butRead" runat="server" Text="Read" onclick="butRead_Click" /><br />
 <asp:Literal ID="litResults" runat="server" />
    </div>

and in code behind :-

protected void butRead_Click(object sender, EventArgs e)
    {
        readXML();
        lblStatus.Text = "";
    }

    private void readXML()
    {
        XDocument xDoc = XDocument.Load(Server.MapPath("~/People.xml"));
        var persons = from person in xDoc.Descendants("Person")
                      select new
                      {
                          Name = person.Element("Name").Value,
                          City = person.Element("City").Value,
                          Age = person.Element("Age").Value,
                      };
        litResults.Text = "";
        foreach (var person in persons)
        {
            litResults.Text = litResults.Text + "Name: " + person.Name + "<br />";
            litResults.Text = litResults.Text + "City: " + person.City + "<br />";
            litResults.Text = litResults.Text + "Age: " + person.Age + "<br /><br />";

        }
    }

    protected void butAdd_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtName.Text == "" || txtCity.Text == "" || txtAge.Text == "")
                lblStatus.Text = "Please complete the form.";
            else
            {
                XDocument xmlDoc = XDocument.Load(Server.MapPath("People.xml"));

                xmlDoc.Element("Persons").Add(new XElement("Person",
                    new XElement("Name", txtName.Text),
                    new XElement("City", txtCity.Text),
                    new XElement("Age", txtAge.Text)));

                xmlDoc.Save(Server.MapPath("People.xml"));
                lblStatus.Text = "Data successfully added to XML file.";
                readXML();
            }
        }
        catch
        {
            lblStatus.Text = "Sorry, unable to process request. Please try again.";
        }
    }

Hope this helps

Good Luck

Categories: Linq, XML

Convert Database tables to XML and Schema

December 24, 2008 Leave a comment

Try this example:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class xml : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string strConnect = null;
        strConnect = ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
        string strSelect = null;
        strSelect = "SELECT * FROM Employees";
        DataSet objDataSet = new DataSet();
        SqlConnection objConnect = new SqlConnection(strConnect);
        SqlDataAdapter objDataAdapter = new SqlDataAdapter(strSelect, objConnect);
        objDataAdapter.Fill(objDataSet, "Employees");

        string strVirtualPath = Request.ApplicationPath + "/Employees.xml";
        string strVSchemaPath = Request.ApplicationPath + "/Employees.xsd";
        objDataSet.WriteXml(Request.MapPath(strVirtualPath));
        objDataSet.WriteXmlSchema(Request.MapPath(strVSchemaPath));
    }
}

Good Luck

Categories: XML