Pages

Saturday, August 17, 2013

How to Read and Write Xml file data in Asp.net

In this article we are explaining how to read and write Xml file data in Asp.net. To use xml file in asp.net we have to use two namespace 'System.Text'  and  'System.Xml'  then follow this steps.

Step 1
Add an Xal file in your project by using 'Add new item'. Then write following code. Xml code can be change according to your need.

<?xml version="1.0" encoding="utf-8"?>
<EmployeeData>
  <Details>
    <Name>Jown</Name>
    <Address>Delhi</Address>
    <Mobile>740579959</Mobile>
  </Details>
</EmployeeData>

Step 2
In 'Default.aspx' add following control.

<form id="form1" runat="server">
        <asp:Label ID="lblName" runat="server" Text="Name:"></asp:Label>
        <asp:TextBox ID="TextName" runat="server" ></asp:TextBox>
        <asp:Label ID="lblAdd" runat="server" Text="Address:"></asp:Label>
        <asp:TextBox ID="TextAdd" runat="server" ></asp:TextBox>
        <asp:Label ID="lblMob" runat="server" Text="Mobile:"></asp:Label>
        <asp:TextBox ID="TextMob" runat="server" ></asp:TextBox>
        <asp:Button ID="WriteXml" runat="server" Text="WriteXml"  OnClick="WriteXml_Click" />
        <asp:Button ID="ReadXml" runat="server" Text="ReadXml"  OnClick="ReadXml_Click" />
        <asp:Label ID="lblMsg" runat="server" Text=""></asp:Label>
    </form>

Step 3
In 'Default.aspx.cs' page add following cod

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class XmlReadWrite : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      
    }
    protected void WriteXml_Click(object sender, EventArgs e)
    {
        XmlTextWriter writeXml = new XmlTextWriter(Server.MapPath("EmpData.xml"), Encoding.UTF8);
        writeXml.WriteStartDocument();
        writeXml.WriteStartElement("EmployeeData");
        writeXml.WriteStartElement("Details");
        writeXml.WriteElementString("Name", TextName.Text);
        writeXml.WriteElementString("Address", TextAdd.Text);
        writeXml.WriteElementString("Mobile", TextMob.Text);
        writeXml.WriteEndElement();
        writeXml.WriteEndElement();
        writeXml.WriteEndDocument();
        writeXml.Close();

    }
    protected void ReadXml_Click(object sender, EventArgs e)
    {
        {
            string parentElement = "";
            string childElement = "";
            string childValue = "";
            bool element = false;
            lblMsg.Text = "";

            XmlTextReader readXml = new XmlTextReader(Server.MapPath("EmpData.xml"));
            while (readXml.Read())
            {
                if (readXml.NodeType == XmlNodeType.Element)
                {
                    if (element)
                    {
                        parentElement = parentElement + childElement + "<br>";
                    }
                    element = true;
                    childElement = readXml.Name;
                }
                else if (readXml.NodeType == XmlNodeType.Text | readXml.NodeType == XmlNodeType.CDATA)
                {
                    element = false;
                    childValue = readXml.Value;
                    lblMsg.Text = lblMsg.Text + "<b>" + parentElement + "<br>" + childElement + "</b><br>" + childValue;
                    parentElement = "";
                    childElement = "";
                }
            }
            readXml.Close();
        }
    }
}

Step 4

Finally run project. Click on 'WriteXml' to write xml and click on 'ResdXml' to read xml.

Friday, August 16, 2013

How to Download and Upload File in Asp.net

In this article we are explaining how to download and Upload file in Asp.net. To download and upload file we have to use an namespace 'System.Net' and 'System.IO' to access file store on local drive at the time of uploading. To upload file visual studio provide 'FileUpload' control by which we can easily upload file.
Code for Download.
  protected void DownloadBtn_Click(object sender, EventArgs e)
    {
        try
        {
            string filePath = "54825/MP9004001913.jpg";
            WebClient wbCli = new WebClient();
            HttpResponse HttpResp = HttpContext.Current.Response;
            HttpResp.Clear();
            HttpResp.ClearContent();
            HttpResp.ClearHeaders();
            HttpResp.Buffer = true;
            HttpResp.AddHeader("Content-Disposition", "attachment;filename=\"" + Server.MapPath(filePath) + "\"");
            byte[] data = wbCli.DownloadData(Server.MapPath(filePath));
            HttpResp.BinaryWrite(data);
            HttpResp.End();
        }
        catch (Exception ex)
        {
        }

    }
Code for Upload.
In 'Default.aspx' add following control.

<form id="form1" runat="server">
    <div>
   
    </div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="UploadBtn" runat="server"  Text="Save" OnClick="Button2_Click" />
        <asp:Label ID="lblMsg" runat="server" Text="Label"></asp:Label>
    </form>
In 'Default.aspx.cs' write following code.
protected void UploadBtn_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            try
            {
                string filename = Path.GetFileName(FileUpload1.FileName);
                FileUpload1.SaveAs(Server.MapPath("~/") + filename);
                lblMsg.Text = "Upload status: File uploaded!";
            }
            catch (Exception ex)
            {
                lblMsg.Text = "error in file uploading: " + ex.Message;
            }
        }

    }