Click here to Skip to main content
12,944,648 members (51,927 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 5 Dec 2011

Passing XML Nodes into Text Box Values and Passing the values from Textbox to XML Nodes

, 5 Dec 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
How to load the values from XML Node into Text Box Controls and Text Box values into XML Nodes with XML Preview

TextBox to XML_File

XML to Textbox_load




As Microsoft technologies have played a vital major role in the development stage, XML file handling is one of the mindblowing features which makes all our users comfortable. Particularly in business users perspective, XML has its own advantage.

In this article, I would like to demonstrate:

  1. To load the Nodes from XML File into the Textbox controls in C# WinForm
  2. The values that we are passing from TextBox will be loaded into Nodes in XML File (i.e., viceversa to point # 1)
  3. Previewing XML File after done with appropriate changes in Rich text box control.


All we need to have is Visual Studio 2005 with VC# .NET and sample XML file.

Using the Code

Here, it has 1 form that is included with 3 Textbox controls, 4 Button controls, 1 Richtextbox control.

  1. Just design the Form as prescribed in the sample image
  2. Make sure that you do provide the appropriate names for the controls which is a good practice
  3. Design the code for the appropriate Buttons

Code Snippet

While including namespace, be sure that you have added System.XML.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace XMLtoTextBoxnViceversa
    public partial class Form1 : Form
        private XmlDocument doc;
        private XmlElement root;
        public string PATH = @"C:\Person.xml";

        public Form1()

        private void Form1_Load(object sender, EventArgs e)


The below code is to obtain the value from Text Box control into XML Nodes in XML file:

//Loads the value from TextBox controls into XML File Nodes
private void btnWritetoXML_Click(object sender, EventArgs e)
    XmlWriter xmlWriter = XmlWriter.Create(PATH);







The below code is to obtain the value from XML Nodes in XML file to Text Box controls in C# Web form:

//Loads the value from XML Nodes into Form - Text Box controls
 private void btnLoadXML_Click(object sender, EventArgs e)
     doc = new XmlDocument();
     root = doc.DocumentElement;
     txtName.Text = root.GetElementsByTagName("Name")[0].InnerText;
     txtAge.Text = root.GetElementsByTagName("Age")[0].InnerText;
     txtAddress.Text = root.GetElementsByTagName("Address")[0].InnerText;

The below code will clear the content from textbox with confirmation window:

//To clear the text box controls and update the XML nodes to be cleared
 private void btnClear_Click(object sender, EventArgs e)
     DialogResult dr = MessageBox.Show("Are you sure to clear the content..?",
    if (dr == DialogResult.Yes)
        txtName.Text = "";
        txtAge.Text = "";
        txtAddress.Text = "";
        rtbPreview.Text = "";

XML File Preview in RichTextBox control written in btnPreview button - click event:

       //To get the preview on XML file in Rich Text Box controls
        private void btnPreview_Click(object sender, EventArgs e)
            rtbPreview.LoadFile(PATH, RichTextBoxStreamType.PlainText);


Points of Interest

Here, handling XML file sounds interesting to work with C# Winforms. Really, you would enjoy XML file handling in a smarter way.


  • 5th December, 2011: Initial version


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


About the Author

Vishnu Prasad C
Software Developer (Senior)
India India
Dear folks.... I am vishnu having 6+ Years of Experience in Microsoft technologies included 2+ from Visual Basic 6.0 and 4+ from Visual Studio .Net framework with SQL Server 2005. I had been in this Code project for last 5 years. I had been explored to various tasks in Microsoft technologies and designated many roles in different organization.

For any queries in .Net technologies, please mail me to

You may also be interested in...

Comments and Discussions

QuestionPassing XML Nodes into edittext Values and Passing the values from edittext to XML Nodes in android Pin
Member 113302774-May-15 23:55
memberMember 113302774-May-15 23:55 
GeneralMy vote of 1 Pin
Selvin5-Dec-11 12:56
memberSelvin5-Dec-11 12:56 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170518.1 | Last Updated 5 Dec 2011
Article Copyright 2011 by Vishnu Prasad C
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid