?
Solved

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

Posted on 2009-03-30
5
Medium Priority
?
802 Views
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 
<person>
<firstname>Danny</firstname>
<lastname>Glover</lastname>
<address> 542 bell south</address>
</person>
<person>
<firstname>Brad</firstname>
<lastname>Smith</lastname>
<address> 508 bell south</address>
</person>
</Family>

Open in new window

0
Comment
Question by:encoredatas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24024558
Something like...          


            XmlDataDocument doc;
            doc = new XmlDataDocument();
           bool exists = true;
            try
            {
                doc.Load("C:/Work//test12.xml");
            }
            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

0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 2000 total points
ID: 24025584
Try this:
            string filename = "family.xml";
            bool exists = File.Exists(filename);
            XmlDocument doc = new XmlDocument();
            if (exists)
            {
                doc.Load(filename);
 
            }
            else
            {
                XmlElement family = doc.CreateElement("family");
                doc.AppendChild(family);
            }
 
                XmlElement person = doc.CreateElement("person");
                XmlElement firstname = doc.CreateElement("firstname");
                XmlElement lastname = doc.CreateElement("lastname");
                XmlElement address = doc.CreateElement("address");
                doc.DocumentElement.AppendChild(person);
                person.AppendChild(firstname);
                person.AppendChild(lastname);
                person.AppendChild(address);
                firstname.InnerText = "Elvis";
                lastname.InnerText = "Presley";
                address.InnerText = "Hollywood";
                doc.Save(filename);

Open in new window

0
 
LVL 30

Expert Comment

by:anarki_jimbel
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
0
 
LVL 30

Expert Comment

by:anarki_jimbel
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();
            ds.ReadXml(filename);
            this.dataGridView1.DataSource = ds.Tables[0];
        }

Open in new window

0
 

Author Closing Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

777 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