Click here to Skip to main content
15,884,237 members
Articles / Desktop Programming / Windows Forms
Article

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

Rate me:
Please Sign up or sign in to vote.
2.83/5 (5 votes)
5 Dec 2011CPOL1 min read 55.2K   1.6K   5   2
How to load the values from XML Node into Text Box Controls and Text Box values into XML Nodes with XML Preview
PassingXMLNodes/Form_appearnce.JPG

TextBox to XML_File

XML to Textbox_load

XMLPreview_filee

Content_Clear

Introduction

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.

Background

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.

C#
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()
        {
            InitializeComponent();
        }

        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:

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

    xmlWriter.WriteStartDocument();
    xmlWriter.WriteStartElement("Person");

    xmlWriter.WriteStartElement("Name");
    xmlWriter.WriteString(txtName.Text);
    xmlWriter.WriteEndElement();

    xmlWriter.WriteStartElement("Age");
    xmlWriter.WriteString(txtAge.Text);
    xmlWriter.WriteEndElement();

    xmlWriter.WriteStartElement("Address");
    xmlWriter.WriteString(txtAddress.Text);
    xmlWriter.WriteEndElement();

    xmlWriter.WriteEndDocument();

    xmlWriter.Close();
}

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

C#
//Loads the value from XML Nodes into Form - Text Box controls
 private void btnLoadXML_Click(object sender, EventArgs e)
 {
     doc = new XmlDocument();
     doc.Load(PATH);
     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:

C#
//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..?",
     "",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
    if (dr == DialogResult.Yes)
    {
        txtName.Text = "";
        txtAge.Text = "";
        txtAddress.Text = "";
        rtbPreview.Text = "";
        txtName.Focus();
    }
 }

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

C#
       //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.

History

  • 5th December, 2011: Initial version

License

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


Written By
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 scnvishnuprasad@gmail.com

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
Member 113302774-May-15 23:55 
GeneralMy vote of 1 Pin
Selvin5-Dec-11 12:56
Selvin5-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.