65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.83/5 (5 votes)

Dec 5, 2011

CPOL

1 min read

viewsIcon

55895

downloadIcon

1566

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.

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:

        //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:

       //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:

       //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:

       //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