• Home
  • Trace mobile number (From India)
  • Entertainment
  • SiteMap
  • Home
  • ASP.NET
  • C#
  • BLOGGING
  • SQL SERVER
  • FACEBOOK
  • Entertainment
Showing posts with label XmlTextReader. Show all posts
Read Write XML File Using C#, VB.NET In Asp.Net 
In this post i am going to tell how you can read and write  XML data from XML file in asp.net using C# .

For Creating this example i have created one aspx page which contains three textbox to enter Customer name, age and location.

And there are two button control one is to write data to xml file and another button to read data from xml file .after reading data it will showed in a label control.

Aspx page code is here 


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ReadWriteXmlData.aspx.cs" Inherits="ReadWriteXmlData" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table border="1 px solid Red" >
    <tr><td colspan="2">Customer Details</td></tr>
    <tr><td >Name:<asp:TextBox ID="txtname" runat="server" Width="200px"></asp:TextBox></td></tr>
    <tr><td >Age:<asp:TextBox ID="txtage" runat="server" Width="200px"></asp:TextBox></td></tr>
    <tr><td >Location:<asp:TextBox ID="txtlocation" runat="server" Width="200px"></asp:TextBox></td></tr>
    <tr><td ><asp:Button ID="btnwrite" runat="server" OnClick="btnwrite_click" Text="Write To Xml" /><asp:Button Text="Read From Xml" ID="btnread" OnClick="btnread_click" runat="server" /></td></tr>
    <tr><td><asp:label id="lblXmlData" runat="server"/></td></tr>
    </table>
    </div>
    </form>
</body>
</html>

CS  Page 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;
using System.Xml;
using System.Text;

public partial class ReadWriteXmlData : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnwrite_click(object o, EventArgs e)
    {
        XmlTextWriter MyWriter = new XmlTextWriter(Server.MapPath("Details.xml"), Encoding.UTF8);
        MyWriter.WriteStartDocument();
        //Create root element Node
        MyWriter.WriteStartElement("CustDetails");
        //Create Child elements
        MyWriter.WriteStartElement("Details");
        MyWriter.WriteElementString("Name", txtname.Text);
        MyWriter.WriteElementString("Age", txtage.Text);
        MyWriter.WriteElementString("Location", txtlocation.Text);
        MyWriter.WriteEndElement();
        //now end top element
        MyWriter.WriteEndElement();
        MyWriter.WriteEndDocument();
        MyWriter.Close();

    }

    protected void btnread_click(object o, EventArgs e)
    {
        string rootElementName = "";
        string childElementName = "";
        string childElementValue = "";
        bool element = false;
        lblXmlData.Text = "";
        XmlTextReader MyxmlReader = new XmlTextReader(Server.MapPath("Details.xml"));
        while (MyxmlReader.Read())
        {
            if (MyxmlReader.NodeType == XmlNodeType.Element)
          {
                if (element)
               {
                   rootElementName = rootElementName + "<br/>" + childElementName + "<br/>";
               }
                element = true;
                childElementName = MyxmlReader.Name;
            }
           else if (MyxmlReader.NodeType == XmlNodeType.Text | MyxmlReader.NodeType == XmlNodeType.CDATA)
            {
                element = false;
                childElementValue = MyxmlReader.Value;
                lblXmlData.Text = lblXmlData.Text + "<b>" + rootElementName + "<br/>" + childElementName + "<br/></b>" + childElementValue;
                rootElementName = "";
                childElementName = "";
            }
        }
        MyxmlReader.Close();
        }
    }
 
Older Posts Home

Most Read

  • How to Capitalize the First Letter of All Words in a string in C# ?
  • keyboard shortcuts For Windows
  • List Of Best Free WordPress Plugins : 2012
  • Read Write XML Data-Read Write XML File Using C#, VB.NET In Asp.Net
  • How to Shake Internet Explorer - Javascript Code
  • How to Choose a Nice Topic for your Blog .
  • Free Search Engine Submission List ,search engine optimization
  • Number validation in Textbox of ASP.NET Using Regular Expression validator
  • Javascript:Percentage Gain Javascript Calculator
  • .Net Interview Questions and Answers on OOPS | OOPS Frequently Asked Questions
Google
Custom Search Bloggers - Meet Millions of Bloggers

Join Us On FaceBook

  • Recent Posts
  • Comments

All Topics

  • ▼  2014 (10)
    • ▼  January (10)
      • ASP.NET Interview Question : difference between ge...
      • Dot Net Framework:What is the .NET Framework?
      • Dot NET Framework - .NET Framework Interview Quest...
      • What is the differences between MVC2,MVC3 and MVC4...
      • Is try catch is using a good coding (exception han...
      • What is the use of Just - In - Time (JIT)?
      • What is GUID , why we use it?,how to create a GUID
      • How to Rename database table column in sqlserver
      • How to Rename Database in sqlserver
      • Asp.net Example Calendar Control
  • ►  2013 (14)
    • ►  October (1)
    • ►  April (2)
    • ►  March (11)
  • ►  2012 (142)
    • ►  December (25)
    • ►  October (1)
    • ►  September (9)
    • ►  August (2)
    • ►  July (7)
    • ►  June (2)
    • ►  April (5)
    • ►  March (27)
    • ►  February (27)
    • ►  January (37)
  • ►  2011 (23)
    • ►  December (3)
    • ►  November (6)
    • ►  October (12)
    • ►  September (2)
  • ►  2009 (1)
    • ►  June (1)

Tips & Tricks

  • What is good One Blog and Many Categories, or Many Blogs with One Categories?
  • Adding Twitter tweet button to each Blogger posts.
  • How to Choose a Nice Topic for your Blog .
  • Embedding YouTube Videos ,movie in your blog
  • Facebook iFrame Apps – Getting Rid of Scrollbars
  • Facebook Analytics:How to Set Up Your Website or Blog with Facebook Insights for Domains
  • How To Add Perfect Share Box to Blogger
  • Blogger Free Images Hosting Tip,Free unlimited bandwidth image hosting for Blogger blogs
  • Add “Link to this post” codes below Each blogger posts
  • Free Search Engine Submission List ,search engine optimization
  • Adsense Tips for Maximum CTR
  • List Of Best Free WordPress Plugins : 2012
2012 tectopix. All rights reserved.
Designed by tectopix