How to insert data into xml file using Windows forms application in C# || Insert and Retrieve data from xml file to bind datagridview in Windows Forms C#


Insert data into XML file :

This article is shows about insert data into XML file instead of using SqlServer database.
For that first Design your form as per your requirements.
In this am adding daily expenses of a person for that am taking fields as Date,  Amount, Purpose, Source  Bank and one Save button like below:



For accessing XML file add the following namespace in top of your code:

                         using System.Xml;

Write the following code in your Save button Click:
private void btnsave_Click(object sender, EventArgs e)
        {

            string path = "Expences.xml";
            XmlDocument doc = new XmlDocument();

            //If there is no current file, then create a new one

            if (!System.IO.File.Exists(path))
            {
                //Create neccessary nodes
                XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
                XmlComment comment = doc.CreateComment("This is an XML Generated File");
                doc.AppendChild(declaration);
                doc.AppendChild(comment);


            }
            else //If there is already a file
            {
                //    //Load the XML File
                doc.Load(path);
            }

            //Get the root element
            XmlElement root = doc.DocumentElement;

            XmlElement Subroot = doc.CreateElement("Expences");
            XmlElement Date = doc.CreateElement("Date");
            XmlElement Amount = doc.CreateElement("Amount");
            XmlElement Purpose = doc.CreateElement("purpose");
            XmlElement Source = doc.CreateElement("Source");
            XmlElement Bank = doc.CreateElement("Bank");

            //Add the values for each nodes
            Date.InnerText = Convert.ToDateTime(dateTimePicker1.Text).ToShortDateString();
            Amount.InnerText = txtAmount.Text;
            Purpose.InnerText = txtPurpose.Text;
            if (radioButton1.Checked)
            {
                Source.InnerText = "Cash";
                Bank.InnerText = " ";
            }
            else
            {
                Source.InnerText = "Bank";
                Bank.InnerText = comboBox1.SelectedItem.ToString();
            }


           
            Subroot.AppendChild(Date);
            Subroot.AppendChild(Amount);
            Subroot.AppendChild(Purpose);
            Subroot.AppendChild(Source);

            Subroot.AppendChild(Bank);
            root.AppendChild(Subroot);
            doc.AppendChild(root);

            //Save the document
            doc.Save(path);


            //Show confirmation message
            MessageBox.Show("Details  added Successfully");

            //Reset text fields for new input
            txtPurpose.Text = String.Empty;
            txtAmount.Text = String.Empty;
        }
       
      Then run your application and see output like below:

To see your saving data in XML file do the following steps:
Select your solution from Solution Explorer
Right click and Select Open Folder in Windows Explorer
Then your application folder will open in that double click on bin folder  then double click debug folder
Then you will see your Expenses XML document file à double click on it then you will see your Saving Data in your browser like below:

Retrieve data from XML file and show in DataGridview :
For showing these saving data into DataGridview, am taking one Show button and DataGridview Control in the form:
Write the following code in show button click:
     private void btnShow_Click(object sender, EventArgs e)
        {
                XmlReader xmlFile;
                xmlFile = XmlReader.Create("Expences.xml"new XmlReaderSettings());
                DataSet ds = new DataSet();
                ds.ReadXml(xmlFile);
                dataGridView1.DataSource = ds.Tables[0];
            }

Then you will see your data binding to DataGridview like below:



21 comments:

  1. the play button is not working

    plz see the issue...

    which is help to viewer....

    ReplyDelete
  2. I think about it is most required for making more on this get engaged UK RDP

    ReplyDelete
  3. Wow, What a Excellent post. I really found this to much informatics. It is what i was searching for.I would like to suggest you that please keep sharing such type of info.Thanks click here

    ReplyDelete
  4. Without fail, your writing style is top professional; even your website also looks amazing thank you for posting. convert pdf to png

    ReplyDelete
  5. Nice to be visiting your blog once more, it has been months for me. Well this article that ive been waited for therefore long. i want this article to finish my assignment within the faculty, and it has same topic together with your article. Thanks, nice share. 먹튀사이트

    ReplyDelete
  6. This is actually the kind of information I have been trying to find. Thank you for writing this information. แทงบอล

    ReplyDelete
  7. Great articles and great layout. Your blog post deserves all of the positive feedback it’s been getting. esa letter

    ReplyDelete
  8. I really like your writing style, great information, thankyou for posting. 메이저사이트

    ReplyDelete
  9. Good post but I was wondering if you could write a litte more on this subject? I’d be very thankful if you could elaborate a little bit further. Appreciate it! 온라인카지노

    ReplyDelete
  10. Great post, you have pointed out some fantastic points , I likewise think this s a very wonderful website. Visual Studio Crack

    ReplyDelete
  11. I really like your writing style, great information, thankyou for posting. 먹튀검증

    ReplyDelete
  12. Good website! I truly love how it is easy on my eyes it is. I am wondering how I might be notified whenever a new post has been made. I have subscribed to your RSS which may do the trick? Have a great day! 안전놀이터

    ReplyDelete
  13. That is the excellent mindset, nonetheless is just not help to make every sence whatsoever preaching about that mather. Virtually any method many thanks in addition to i had endeavor to promote your own article in to delicius nevertheless it is apparently a dilemma using your information sites can you please recheck the idea. thanks once more. 먹튀검증업체

    ReplyDelete
  14. I read your blog frequently and I just thought I’d say keep up the amazing work! 먹튀검증

    ReplyDelete
  15. I am sure this article has touched all the internet viewers, its really really
    pleasant piece of writing on building up new website. 바카라사이트
    (mm)

    ReplyDelete
  16. Good website! I truly love how it is easy on my eyes it is. I am wondering how I might be notified whenever a new post has been made. I have subscribed to your RSS which may do the trick? Have a great day! 대전건마

    ReplyDelete
  17. Very good points you wrote here..Great stuff...I think you've made some truly interesting points.Keep up the good work. textbook answers

    ReplyDelete
  18. I am glad you take pride in what you write. This makes you stand way out from many other writers that push poorly written content. 대전건마

    ReplyDelete
  19. What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much. Meridian

    ReplyDelete
  20. It should be noted that whilst ordering papers for sale at paper writing service, you can get unkind attitude. In case you feel that the bureau is trying to cheat you, don't buy term paper from it. mvp

    ReplyDelete
  21. Share large files up to 5 GB free using email or a secure link File share

    ReplyDelete