Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    APIs
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> XML >> How to read and write XML in C#
How to read and write XML in C#
How to read and write XML in C#


by Sachindra Singh on 1/27/2011 6:45:23 PM

Views: 19113       Comments: 2

How to read and write XML in C#

XML (Extensible Markup Language) is a flexible way to create common information formats and share both the format and the data on the World Wide Web, intranets, and elsewhere. XML is a set of rules, published by the World Wide Web Consortium, for building new languages. The languages in question are not written or spoken primarily for human consumption.

This demonstration showing how can we save color setting of textboxes and get color setting of textboxes from xml file, we change backcolor and foreColor of textbox and when we want when our application will be run on the system BackColor and foreColor of textboxes should be previously color setting, it means previous color setting of textbox should display, this demonstration will help to save color setting of textbox in XML format and also get color from XML file.

Code

  public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        ColorDialog cd = new ColorDialog();//creating the instance of ColorDialog

        int flag = 0;//creating int variable

        private void Form1_Load(object sender, EventArgs e)

        {

          

            XmlReaderSettings setting = new XmlReaderSettings();//creating object of xmlReaderSetting class it will read xml file

            setting.IgnoreWhitespace = true;//ignoring whit space in xml file

            using (XmlReader reader = XmlReader.Create("D:\\Sachindra\\ColorChange\\ColorChange\\bin\\Debug\\ColorChange.xml", setting))//providing path where xml file is save

            {

                while (reader.Read())//loop executing

                {

                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "txtBox1BackColor")//checking condition

                    {

 

                        string strCmb1 = reader.ReadElementString();//storing value of txtBox1BackColor element(backcolor) in string variable

                        txtBox1.BackColor = System.Drawing.ColorTranslator.FromHtml(strCmb1);//converting string value  into color and changing backcolor of textbox

                    }

                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "txtBox1ForeColor")//checking condition

                    {

 

                        string strCmb1 = reader.ReadElementString();//storing value of txtBox1BackColor element (backcolor) in string variable

                        txtBox1.ForeColor = System.Drawing.ColorTranslator.FromHtml(strCmb1);//converting string value  into color and changing forecolor of textbox

                      

                    }

                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "txtBox2BackColor")//checking condition

                    {

 

                        string strCmb1 = reader.ReadElementString();//storing value of txtBox2BackColor element(backcolor) in string variable

                        txtBox2.BackColor = System.Drawing.ColorTranslator.FromHtml(strCmb1);//converting string value  into color and changing backcolor of textbox

                     

                    }

                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "txtBox2ForeColor")//checking condition

                    {

 

                        string strCmb1 = reader.ReadElementString();//storing value of txtBox2ForeColor element in string variable

                        txtBox2.ForeColor = System.Drawing.ColorTranslator.FromHtml(strCmb1);//converting string value  into color and changing backcolor of textbox

                 

                    }

                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "txtBox3BackColor")//checking condition

                    {

 

                        string strCmb1 = reader.ReadElementString();//storing value of txtBox3BackColor element in string variable

                        txtBox3.BackColor = System.Drawing.ColorTranslator.FromHtml(strCmb1);//converting string value  into color and changing backcolor of textbox

                    

                    }

                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "txtBox3ForeColor")//checking condition

                    {

 

                        string strCmb1 = reader.ReadElementString();//storing value of txtBox3ForeColor element in string variable

                        txtBox3.ForeColor = System.Drawing.ColorTranslator.FromHtml(strCmb1);//converting string value  into color and changing backcolor of textbox

                 

                    }

                }

            }

        }

 

        private void btnColorDialog1_Click(object sender, EventArgs e)

        {

            if (flag == 0)//checking condition

            {

                cd.ShowDialog();//it will show color dialog

                txtBox1.BackColor = cd.Color;//setting backcolor of textbox

            }

            if (flag == 1)//checking condtion

            {

                cd.ShowDialog();//it will show color dialog

                txtBox1.ForeColor = cd.Color;//setting forecolor of textbox

                flag = 0;//initializing flag from 0

            }

        }

 

        private void txtBox1_MouseCaptureChanged(object sender, EventArgs e)

        {

            flag = 1;//initializing flag from 1 when txtBox1 get  MouseCapture event will raise

        }

 

        private void btnColorDailog2_Click(object sender, EventArgs e)

        {

            if (flag == 0)//checking condition

            {

                cd.ShowDialog();//it will show color dialog

                txtBox2.BackColor = cd.Color;//setting backcolor of textbox

            }

            if (flag == 1)//checking condtion

            {

                cd.ShowDialog();//it will show color dialog

                txtBox2.ForeColor = cd.Color;//setting forecolor of textbox

                flag = 0;//initializing flag from 0

            }

        }

 

        private void txtBox2_MouseCaptureChanged(object sender, EventArgs e)

        {

            flag = 1;//initializing flag from 1 when txtBox2 get MouseCapture event will raise

        }

 

        private void btnColorDialog3_Click(object sender, EventArgs e)

        {

            if (flag == 0)//checking condition

            {

                cd.ShowDialog();//it will show color dialog

                txtBox3.BackColor = cd.Color;//setting backcolor of textbox

            }

            if (flag == 1)//checking condtion

            {

                cd.ShowDialog();//it will show color dialog

                txtBox3.ForeColor = cd.Color;//setting forecolor of textbox

                flag = 0;//initializing flag from 0

            }

        } 

        private void txtBox3_MouseCaptureChanged(object sender, EventArgs e)

        {

            flag = 1;//initializing flag from 1 when txtBox3 get MouseCapture event will raise

        } 

        private void btn_Click(object sender, EventArgs e)

        {

            XmlWriterSettings setting = new XmlWriterSettings();//creating object of XmlWriterSettings that write data in xml format

            setting.Indent = true;

            setting.IndentChars = " ";

            setting.NewLineOnAttributes = true;

            using (XmlWriter write = XmlWriter.Create("D:\\Sachindra\\ColorChange\\ColorChange\\bin\\Debug\\ColorChange.xml", setting))//passing path and  write storing path where file will be write

            {

                write.WriteStartElement("Color");//root element

                write.WriteStartElement("ChangeColor");//element

                write.WriteElementString(txtBox1.Name + "BackColor", System.Drawing.ColorTranslator.ToHtml(txtBox1.BackColor));//writing textbox name and backcolor in xml format

                write.WriteElementString(txtBox1.Name + "ForeColor", System.Drawing.ColorTranslator.ToHtml(txtBox1.ForeColor));//writing textbox name and forecolor in xml format

                write.WriteElementString(txtBox2.Name + "BackColor", System.Drawing.ColorTranslator.ToHtml(txtBox2.BackColor));//writing textbox name and backcolor in xml format

                write.WriteElementString(txtBox2.Name + "ForeColor", System.Drawing.ColorTranslator.ToHtml(txtBox2.ForeColor));//writing textbox name and forecolor in xml format

                write.WriteElementString(txtBox3.Name + "BackColor", System.Drawing.ColorTranslator.ToHtml(txtBox3.BackColor));//writing textbox name and backcolor in xml format

                write.WriteElementString(txtBox3.Name + "ForeColor", System.Drawing.ColorTranslator.ToHtml(txtBox3.ForeColor));//writing textbox name and forecolor in xml format

                write.WriteEndElement();//closing element

                write.Flush();//flush method use for forcefully to write data

            }

        } 

        private void button1_Click(object sender, EventArgs e)

        {

            this.Close();//apllication will be close

        }

    } 

This demonstration showing backcolor and forecolor of texboxes has changed and when we click on save button it will save in XML format.

how to read and write xml in c#.net

 

After color changes of textboxes XML file will save setting of backcolor and forecolor of textboxes in XML format as shown below:

how to read and write xml in c#.net

This is the simple demonstration on "how to read xml and write xml in c#.net".

Report Abuse Form
Reason:    
 


Re: How to read and write XML in C#
by Arun Singh 10/13/2011 2:47:21 AM
Hey Sachindra,
this is great stuff, thanks for sharing with us.
Report Abuse

Nice Article
by aken H 10/20/2011 7:21:12 PM

Hi Sechindra,

     this a nice article,I hope you release more good articles in the near.

thanks!

Report Abuse
Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Sachindra SinghRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 6693
Advertisement
MindStick Cleaner
Advertise with Us
  
Copyright © 2013MindStick. All Rights Reserved.