School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,103 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 4,756 people online right now. Registration is fast and FREE... Join Now!



Working with XML files in C# - Part 2

Page 1 of 1

Working with XML files in C# - Part 2 Adding nodes, removing nodes, adding attributes, and changing attribut

#1 aj32  Icon User is offline

  • D.I.C Addict
  • Icon
  • Group: Author w/DIC++
  • Posts: 577
  • Joined: 30-August 07


Dream Kudos: 675

Post icon  Posted 16 April 2008 - 04:31 PM

Working with XML files in C# - part 2

Adding nodes, removing nodes, adding attributes, and changing attributes


In a previous tutorial, I explained how to read and change nodes in XML files, this is part two of that tutorial!
If you have not yet had a chance to read my other tutorial, please read it before continuing (here)

For this tutorial, I am going to set up a form like below:

Posted Image

On that form is a listbox and two buttons.

I am going to use an xml file that is structured like this one:

<?xml version="1.0"?>
<config>
  <Item>This is item #1!</Item>
  <Item>This is item #2!</Item>
  <Item>This is item #3!</Item>
</config>



To read this xml document, and put it's contents in our listbox, I will use this code in Form1_load:
private void Form1_Load(object sender, EventArgs e)
        {
            //Clear all of the items out of the list box
            listBox1.Items.Clear();

            //the path to the xml file
            string path = "Config.xml";

            //Open the filestream
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            
            //create the xmldocument
            System.Xml.XmlDocument CXML = new System.Xml.XmlDocument();

            //load the xml into the XmlDocument
            CXML.Load(fs);

                                //Get the number of nodes in the xml document
            for (int i = 0; i < CXML.DocumentElement.ChildNodes.Count; i++)
            {
                //Add the innertext of the xml document to the listbox
                listBox1.Items.AddRange(new object[] { CXML.DocumentElement.ChildNodes[i].InnerText });
            }

            //Close the filestream
            fs.Close();
        }




Now that we have that part of our program set up, let's continue with the adding and removing of nodes!



Section 1: Adding nodes


To add nodes to an xml document, we need to:
  • Open the file
  • Load the xml into a System.xml.xmlDocument
  • Create a new xml element (or node)
  • Load the InnerText of the new element
  • Insert the new element into the XmlDocument
  • Save the XmlDocument


To do this, we will create an event for our 'Add' button, in that event, we will type in code like this:
//The path to our config file
                string path = "Config.xml";

                //create the reader filestream (fs)
                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                
                //Create the xml document
                System.Xml.XmlDocument CXML = new System.Xml.XmlDocument();

                //Load the xml document
                CXML.Load(fs);

                //Close the fs filestream
                fs.Close();


                // create the new element (node)
                XmlElement newitem = CXML.CreateElement("Item");

                // Put the value (inner Text) into the node
                newitem.InnerText = "This is item #" + (CXML.DocumentElement.ChildNodes.Count + 1).ToString() + "!";


                //Insert the new XML Element into the main xml document (CXML)
                CXML.DocumentElement.InsertAfter(newitem, CXML.DocumentElement.LastChild);

                //Save the XML file
                FileStream WRITER = new FileStream(path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
                CXML.Save(WRITER);

                //Close the writer filestream
                WRITER.Close();

                //Update the contents of the listbox
                Form1_Load(new object(),  new EventArgs());




In detail, here is how we do it:

In this code:
//create the reader filestream (fs)
                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);


We are creating the filestream that will read our xml document, the filestream has 4 arguments:
  • The path of the document to open
  • FileAccess type - how the file is opened - there are a few different argument for this one
  • How to open it - this can be set to Open, Write, or OpenWrite
  • How to share the file with other applications while open


In this part:
// create the new element (node)
                XmlElement newitem = CXML.CreateElement("Item");

                // Put the value (inner Text) into the node
                newitem.InnerText = "This is item #" + (CXML.DocumentElement.ChildNodes.Count + 1).ToString() + "!";


we are creating the new node to be inserted into the xml document.

Next,
//Insert the new XML Element into the main xml document (CXML)
                CXML.DocumentElement.InsertAfter(newitem, CXML.DocumentElement.LastChild);


simply inserts the new node into the xml document.

At the end, we of course save the xml document to the file, notice the two changes since the first filestream:
  • The FileMode has been changed to FileMode.Truncate <- that will empty the xml file before saving
  • The FileAccess has been changed to FileAccess.Write <-Enables writing to the file




Section 2: Removing nodes
Now that we know how to read and write xml file and add nodes, we will now discuss how to remove a node.

Removing a node is much like adding a node:
  • Open the xml file
  • Load the xml into a System.xml.xmlDocument
  • Remove the node from the XmlDocument
  • Save the XmlDocument to the xml file

Here's the code:
if (listBox1.SelectedIndex > -1)
            {
                //The Path to the xml file
                string path = "config.xml";

                //Create FileStream fs
                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                //Create new XmlDocument
                System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();

                //Load the contents of the filestream into the XmlDocument (xmldoc)
                xmldoc.Load(fs);

                //close the fs filestream
                fs.Close();

                //Remove the xml node
                xmldoc.DocumentElement.RemoveChild(xmldoc.DocumentElement.ChildNodes[listBox1.SelectedIndex]);

                // Create the filestream for saving
                FileStream WRITER = new FileStream(path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
               
                // Save the xmldocument
                xmldoc.Save(WRITER);

                //Close the writer filestream
                WRITER.Close();

                //Update the contents of the listbox
                Form1_Load(new object(), new EventArgs());
            
            }



In this code, first we are making sure that the user has selected an item in the listbox. Next the program opens the file, loads the file into XmlDocument xmldoc, removes the node from the XmlDocument, and saves it to the xml file.

Again, you can see the differences in how I am opening my filestreams.




Section 3: Adding an xml attribute
To add an attribute is rather simple:
  • Open the xml file
  • Load the xml into a System.xml.xmlDocument
  • Create the new Attribute
  • Set the new Attribute's inner text
  • insert the attribute into the xmldocument
  • Save the XmlDocument to the xml file

In this code, it will add an attribute to the first xml node
//The Path to the xml file
                string path = "config.xml";

                //Create FileStream fs
                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                //Create new XmlDocument
                System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();

                //Load the contents of the filestream into the XmlDocument (xmldoc)
                xmldoc.Load(fs);

                //close the fs filestream
                fs.Close();

                //Create the new xml attribute
                XmlAttribute xmlat1 = xmldoc.CreateAttribute("ID");
                xmlat1.InnerText = "001";

                //Insert the xml attribute
                xmldoc.DocumentElement.ChildNodes[0].Attributes.Append(xmlat1);

                // Create the filestream for saving
                FileStream WRITER = new FileStream(path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);

                // Save the xmldocument
                xmldoc.Save(WRITER);

                //Close the writer filestream
                WRITER.Close();






Section 4: Changing the contents of an xml attribute

To change an attribute of an xml node, we need to:
  • Open the xml file
  • Load the xml into a System.xml.xmlDocument
  • Set the new Attribute's inner text
  • Save the XmlDocument to the xml file

Here's how:
//The Path to the xml file
            string path = "config.xml";

            //Create FileStream fs
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            //Create new XmlDocument
            System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();

            //Load the contents of the filestream into the XmlDocument (xmldoc)
            xmldoc.Load(fs);

            //close the fs filestream
            fs.Close();

            //Change the contents of the attribute
            xmldoc.DocumentElement.ChildNodes[0].Attributes[0].InnerText = "002";

            // Create the filestream for saving
            FileStream WRITER = new FileStream(path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);

            // Save the xmldocument
            xmldoc.Save(WRITER);

            //Close the writer filestream
            WRITER.Close();



Assuming that there is an attribute for the first xml node, this will change it to 002.




That is the end of this XML tutorial, I hope it was useful to you, and thanks for reading!
-AJ32 B)
Was This Post Helpful? 0
  • +
  • -


#2 KsiushA  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 1
  • Joined: 20-May 09


Dream Kudos: 0

Posted 20 May 2009 - 01:04 PM

It didn't work :(

Error 2 The best overloaded method match for 'System.Web.UI.WebControls.ListItemCollection.AddRange(System.Web.UI.WebControls.ListItem[])' has some invalid arguments C:\Documents and Settings\Allea\My Documents\Visual Studio 2005\WebSites\WebSite1\Default.aspx.cs 40 17 C:\...\WebSite1\

for this line:

//Add the innertext of the xml document to the listbox
ListBox1.Items.AddRange(new object[] { CXML.DocumentElement.ChildNodes[i].InnerText});


:-/ What's wrong :-/
Was This Post Helpful? 0
  • +
  • -

#3 StickyTape  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 3
  • Joined: 15-October 09


Dream Kudos: 0

Posted 16 October 2009 - 12:57 AM

[size=6][quote name='aj32' date='16 Apr, 2008 - 04:31 PM' post='341948']
[size=6][b][u]Working with XML files in C# - part 2[/u][/b][/size]

[size=3][i]Adding nodes, removing nodes, adding attributes, and changing attributes[/i][/size]


In a previous tutorial, I explained how to read and change nodes in XML files, this is part two of that tutorial!
If you have not yet had a chance to read my other tutorial, please read it before continuing ([url=http://www.dreamincode.net/forums/showtopic48954.htm]here[/url])

For this tutorial, I am going to set up a form like below:

[img]http://www.imagehostdirect.com/images/aj32/8mg1.jpg[/img]

On that form is a listbox and two buttons.

I am going to use an xml file that is structured like this one:

[code=xml]
<?xml version="1.0"?>
<config>
  <Item>This is item #1!</Item>
  <Item>This is item #2!</Item>
  <Item>This is item #3!</Item>
</config>



To read this xml document, and put it's contents in our listbox, I will use this code in Form1_load:
private void Form1_Load(object sender, EventArgs e)
        {
            //Clear all of the items out of the list box
            listBox1.Items.Clear();

            //the path to the xml file
            string path = "Config.xml";

            //Open the filestream
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            
            //create the xmldocument
            System.Xml.XmlDocument CXML = new System.Xml.XmlDocument();

            //load the xml into the XmlDocument
            CXML.Load(fs);

                                //Get the number of nodes in the xml document
            for (int i = 0; i < CXML.DocumentElement.ChildNodes.Count; i++)
            {
                //Add the innertext of the xml document to the listbox
                listBox1.Items.AddRange(new object[] { CXML.DocumentElement.ChildNodes[i].InnerText });
            }

            //Close the filestream
            fs.Close();
        }




Now that we have that part of our program set up, let's continue with the adding and removing of nodes!



Section 1: Adding nodes


To add nodes to an xml document, we need to:
  • Open the file
  • Load the xml into a System.xml.xmlDocument
  • Create a new xml element (or node)
  • Load the InnerText of the new element
  • Insert the new element into the XmlDocument
  • Save the XmlDocument
To do this, we will create an event for our 'Add' button, in that event, we will type in code like this:
//The path to our config file
                string path = "Config.xml";

                //create the reader filestream (fs)
                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                
                //Create the xml document
                System.Xml.XmlDocument CXML = new System.Xml.XmlDocument();

                //Load the xml document
                CXML.Load(fs);

                //Close the fs filestream
                fs.Close();


                // create the new element (node)
                XmlElement newitem = CXML.CreateElement("Item");

                // Put the value (inner Text) into the node
                newitem.InnerText = "This is item #" + (CXML.DocumentElement.ChildNodes.Count + 1).ToString() + "!";


                //Insert the new XML Element into the main xml document (CXML)
                CXML.DocumentElement.InsertAfter(newitem, CXML.DocumentElement.LastChild);

                //Save the XML file
                FileStream WRITER = new FileStream(path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
                CXML.Save(WRITER);

                //Close the writer filestream
                WRITER.Close();

                //Update the contents of the listbox
                Form1_Load(new object(),  new EventArgs());




In detail, here is how we do it:

In this code:
//create the reader filestream (fs)
                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);


We are creating the filestream that will read our xml document, the filestream has 4 arguments:
  • The path of the document to open
  • FileAccess type - how the file is opened - there are a few different argument for this one
  • How to open it - this can be set to Open, Write, or OpenWrite
  • How to share the file with other applications while open
In this part:
// create the new element (node)
                XmlElement newitem = CXML.CreateElement("Item");

                // Put the value (inner Text) into the node
                newitem.InnerText = "This is item #" + (CXML.DocumentElement.ChildNodes.Count + 1).ToString() + "!";


we are creating the new node to be inserted into the xml document.

Next,
//Insert the new XML Element into the main xml document (CXML)
                CXML.DocumentElement.InsertAfter(newitem, CXML.DocumentElement.LastChild);


simply inserts the new node into the xml document.

At the end, we of course save the xml document to the file, notice the two changes since the first filestream:
  • The FileMode has been changed to FileMode.Truncate <- that will empty the xml file before saving
  • The FileAccess has been changed to FileAccess.Write <-Enables writing to the file
Section 2: Removing nodes
Now that we know how to read and write xml file and add nodes, we will now discuss how to remove a node.

Removing a node is much like adding a node:
  • Open the xml file
  • Load the xml into a System.xml.xmlDocument
  • Remove the node from the XmlDocument
  • Save the XmlDocument to the xml file
Here's the code:
if (listBox1.SelectedIndex > -1)
            {
                //The Path to the xml file
                string path = "config.xml";

                //Create FileStream fs
                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                //Create new XmlDocument
                System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();

                //Load the contents of the filestream into the XmlDocument (xmldoc)
                xmldoc.Load(fs);

                //close the fs filestream
                fs.Close();

                //Remove the xml node
                xmldoc.DocumentElement.RemoveChild(xmldoc.DocumentElement.ChildNodes[listBox1.SelectedIndex]);

                // Create the filestream for saving
                FileStream WRITER = new FileStream(path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
               
                // Save the xmldocument
                xmldoc.Save(WRITER);

                //Close the writer filestream
                WRITER.Close();

                //Update the contents of the listbox
                Form1_Load(new object(), new EventArgs());
            
            }



In this code, first we are making sure that the user has selected an item in the listbox. Next the program opens the file, loads the file into XmlDocument xmldoc, removes the node from the XmlDocument, and saves it to the xml file.

Again, you can see the differences in how I am opening my filestreams.




Section 3: Adding an xml attribute
To add an attribute is rather simple:
  • Open the xml file
  • Load the xml into a System.xml.xmlDocument
  • Create the new Attribute
  • Set the new Attribute's inner text
  • insert the attribute into the xmldocument
  • Save the XmlDocument to the xml file
In this code, it will add an attribute to the first xml node
//The Path to the xml file
                string path = "config.xml";

                //Create FileStream fs
                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                //Create new XmlDocument
                System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();

                //Load the contents of the filestream into the XmlDocument (xmldoc)
                xmldoc.Load(fs);

                //close the fs filestream
                fs.Close();

                //Create the new xml attribute
                XmlAttribute xmlat1 = xmldoc.CreateAttribute("ID");
                xmlat1.InnerText = "001";

                //Insert the xml attribute
                xmldoc.DocumentElement.ChildNodes[0].Attributes.Append(xmlat1);

                // Create the filestream for saving
                FileStream WRITER = new FileStream(path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);

                // Save the xmldocument
                xmldoc.Save(WRITER);

                //Close the writer filestream
                WRITER.Close();






Section 4: Changing the contents of an xml attribute

To change an attribute of an xml node, we need to:
  • Open the xml file
  • Load the xml into a System.xml.xmlDocument
  • Set the new Attribute's inner text
  • Save the XmlDocument to the xml file
Here's how:
//The Path to the xml file
            string path = "config.xml";

            //Create FileStream fs
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            //Create new XmlDocument
            System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();

            //Load the contents of the filestream into the XmlDocument (xmldoc)
            xmldoc.Load(fs);

            //close the fs filestream
            fs.Close();

            //Change the contents of the attribute
            xmldoc.DocumentElement.ChildNodes[0].Attributes[0].InnerText = "002";

            // Create the filestream for saving
            FileStream WRITER = new FileStream(path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);

            // Save the xmldocument
            xmldoc.Save(WRITER);

            //Close the writer filestream
            WRITER.Close();



Assuming that there is an attribute for the first xml node, this will change it to 002.




That is the end of this XML tutorial, I hope it was useful to you, and thanks for reading!
-AJ32 B)
[/quote][/size][code]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1


Fast Reply

  

5 User(s) are reading this topic
0 members, 5 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month