Click here to Skip to main content
15,891,033 members
Articles / Programming Languages / C# 3.5
Tip/Trick

Add New User Registration Information in XML with C#

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
14 Nov 2014CPOL 15.9K   3   2
Add new user registeration information in XML with C#

Introduction

This tip states how you add new user information in XML document if XML document already exists, then append in it. Please gain some knowledge about XmlDocument and XmlNodeList for better understanding.

Using the Code

Step 1: Open Visual Studio -> Create New Website-> click Empty ASP.NET website

Step 2: In Solution Explorer, right click add new component-> Add new webform

Step 3: Create a simple registration form with Username, Age ,Mobile, Password Fields and a Login Button

Step 4: Add an XML file named XMLRegister.xml

Step 5: Double click on Login Button

C#
public partial class Register : System.Web.UI.Page
{
    static string name=string.Empty;
    static string pswd=string.Empty;
    static string age=string.Empty;
    static string mobile=string.Empty;
    static XmlNode root;
    static XmlNode xn; XmlDocument xdoc; 

protected void Page_Load(object sender, EventArgs e)
    {
        lblErrorPswd.Visible = false;
        name = txtUname.Text;
        if(Page.IsPostBack)
      {
          //confirm Password 
        if (txtPswd.Text == txtCpswd.Text)
         {
            pswd = txtPswd.Text;
            age = txtAge.Text; 
            mobile=txtMobile.Text;
         }
       else
         {
         lblError.Visible=True;
          }
      }    
    }
protected void butRegister_Click(object sender, EventArgs e)
    {
   string xmlpath = @"C:\Users\XMlRegister.xml";
   xdoc = new XmlDocument();
        
//XmlDeclartion class used to declare XML prolog
    XmlDeclaration xdeclartion = xdoc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
            root = xdoc.CreateElement("logindetails");
            xdoc.AppendChild(root);
            AppendXml(root);
            xdoc.Save(xmlpath);
            Response.Redirect("login.aspx");
        }
//Add Details
protected void AppendXml(XmlNode root)
    {
        XmlNode person = xdoc.CreateElement("person");
        root.AppendChild(person);
        XmlNode uname = xdoc.CreateElement("uname");
        uname.InnerText = name;
        person.AppendChild(uname);
        XmlNode psswd = xdoc.CreateElement("pswd");
        psswd.InnerText = pswd;
        person.AppendChild(psswd);
        XmlNode agge = xdoc.CreateElement("age");
        agge.InnerText = age;
        person.AppendChild(agge);
        XmlNode mob = xdoc.CreateElement("mobile");
        mob.InnerText = mobile;
        person.AppendChild(mob);
    }

Points of Interest

  • Use XmlWriter or Xelement for better performance

History

This is my first post on CodeProject, if there are some errors or flaws, please write in the comments section below. The XML source data and the knowledge comes from the web and MSDN, I just wrote a demo app to show them. No copyright reserved.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNice article but Pin
Salam Y. ELIAS25-Nov-14 13:06
professionalSalam Y. ELIAS25-Nov-14 13:06 
BugI think you have a typo in your code "agge" Pin
PinnPropHead17-Nov-14 8:19
PinnPropHead17-Nov-14 8:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.