How to display XML File records in Label control Label in ASP.Net

Last Reply on Dec 20, 2013 06:50 AM By Azim

Posted on Dec 20, 2013 06:46 AM

I have XML file named as "XMLFile.xml"

So how to write XML records in Label??

can someone help me,please???

Thanks in advance.

Posted on Dec 20, 2013 06:50 AM Modified on on Dec 20, 2013 09:49 AM

This Way:

XML:

<Employees>
  <Details>
    <Name>
      John
    </Name>
  </Details>
  <Details>
    <Name>
      Smith
    </Name>
  </Details>
  <Details>
    <Name>
      Micheal
    </Name>
  </Details>
</Employees>

HTML:

<form id="form1" runat="server">
    <div>
Name of Emplyees
    <br />
  <asp:Label ID="lblName" Text="" runat="server" /> </div> </form>

C#:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                using (DataSet ds = new DataSet())
                {
                    ds.ReadXml(Server.MapPath("XMLFile1.xml"));
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        lblName.Text += ds.Tables["Details"].Rows[i]["Name"].ToString() + "<br />";
                    }
                }
            }
        }

OutPut:

Thank You.