Creating XML file in Winforms to Load into a DataGrid in C# part 1

Posted on 2009-03-30
Last Modified: 2012-05-06
I need to write a function in C# that checks if an XML file exists at a certain path. If the file exists I need to append child nodes(person) to the XML file and save it.  If it doesn't exists, I need to create The XML file , then append the child nodes(person) to this and save it.  I will later load the XML file using another form that contains a DataGrid so the file can be viewed in a grid style layout.  How would I write a function to do this? The part 2 of this question I will put in a separate question as far as loading the DataGrid. After a solution is found to this one. Thanks for any help in advance.
<Family> // root 
<address> 542 bell south</address>
<address> 508 bell south</address>

Open in new window

Question by:encoredatas
  • 3
LVL 12

Expert Comment

ID: 24024558
Something like...          

            XmlDataDocument doc;
            doc = new XmlDataDocument();
           bool exists = true;
            catch (System.IO.FileNotFoundException fe)
                // File not found
                exists = false;
                doc.CreateElement( "Family" );
            XmlDocument doc2 = new XmlDocument();
            doc2.LoadXml( "<person......");
            doc.AddChild ( doc2.DocumentElement );
           doc.Save ();

Open in new window

LVL 29

Accepted Solution

anarki_jimbel earned 500 total points
ID: 24025584
Try this:
            string filename = "family.xml";
            bool exists = File.Exists(filename);
            XmlDocument doc = new XmlDocument();
            if (exists)
                XmlElement family = doc.CreateElement("family");
                XmlElement person = doc.CreateElement("person");
                XmlElement firstname = doc.CreateElement("firstname");
                XmlElement lastname = doc.CreateElement("lastname");
                XmlElement address = doc.CreateElement("address");
                firstname.InnerText = "Elvis";
                lastname.InnerText = "Presley";
                address.InnerText = "Hollywood";

Open in new window

LVL 29

Expert Comment

ID: 24025593
I tested the code and it works. However it does not validates the xml. If xml is not valid - we'll get an error. So some additional precautions should be taken. E.g. put doc.Load(filename)  into try-catch
LVL 29

Expert Comment

ID: 24025725
Bonus :)

Really, I probably can give you a solution to part 2. The easiest way to display your xml in a grid is to use a dataset. If you want to just display (not edit) - set e.g. datagridview1.editmode = editprogrammatically
        private void button1_Click(object sender, EventArgs e)
            string filename = "family.xml";
            DataSet ds = new DataSet();
            this.dataGridView1.DataSource = ds.Tables[0];

Open in new window


Author Closing Comment

ID: 31564600
Thanks so much this work like a charm and saved me a lot of time. P. S Thanks for the bonus.

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
System.Speech 2 28
Adding  DYMO Labelprinter to c# client application 4 36
Expression Evaluater 3 27
What Does This C# Code Block Do? 5 33
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question