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

How to read and write XML in C#


XML XML 
Ratings:
2 Comment(s)
 35961  View(s)
Rate this:

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

    {

        publicForm1()

        {

            InitializeComponent();

        }

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

        intflag=0;//creating int variable

        private voidForm1_Load(objectsender, EventArgse)

        {

          

            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, EventArgse)

        {

            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, EventArgse)

        {

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

        }

 

        private void btnColorDailog2_Click(object sender, EventArgse)

        {

            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, EventArgse)

        {

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

        }

 

        private void btnColorDialog3_Click(object sender, EventArgse)

        {

            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, EventArgse)

        {

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

       

        private voidbtn_Click(objectsender, EventArgse)

        {

            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 voidbutton1_Click(objectsender, EventArgse)

        {

            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".


Re: How to read and write XML in C#

By Arun Singh on   5 years ago
Hey Sachindra,
this is great stuff, thanks for sharing with us.

Nice Article

By aken H on   5 years ago

Hi Sechindra,

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

thanks!


Don't want to miss updates? Please click the below button!

Follow MindStick