+ Reply to Thread
Results 1 to 2 of 2

Thread: C# XML read , write , modify , delete elements

  1. #1

    C# XML read , write , modify , delete elements

    Hi , i am new to XML but however i understand it , i just wanna know how can i manipulate XML using C# ?
    Like Reading , Writing , Modifying and Deleting elements from XML.
    Last edited by csharpmaster; 11-09-2012 at 12:42 AM.

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    22
    Sample XML file :
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    < settings>
       <AlarmFileLocation>files/default.wav</AlarmFileLocation>
       <Notifications>True</Notifications>
       <AutoLogin>On</AutoLogin>
    < /settings>
    Read XML data :
    <><><><><><>
    Steps :
    1- choose the xml file location and load it
    2- Create a list of Nodes
    3- loop inside it to extract data

    Things must know first :
    InnerText >> the text inside the element
    SelectNodes("//settings") >> that will make a list of nodes inside the settings tag
    SelectSingleNode("Notifications") >> that will retrieve the value of a certain node only
    And here is the code :
    Code:
                XmlDocument xml = new XmlDocument();
                xml.Load("files/settings.xml");
    
                XmlNodeList reminders = xml.SelectNodes("//settings");
    
                foreach (XmlNode reminder in reminders)
                {
                    if (reminder.SelectSingleNode("Notifications").InnerText == "True")
                    {
                        MessageBox.Show("Notifications are On");
                    }
                    else if (reminder.SelectSingleNode("Notifications").InnerText == "False")
                    {
                        MessageBox.Show("Notifications are Off");
                    }
                }
    Create a new XML Node :
    <><><><><><><><><><>
    Steps :
    1- choose the xml file location and load it
    2- create a category
    3- add elements to it like firstelement & secondelement
    5- give both an innertext value
    5- append them after the last child

    Here is the code :
    Code:
                XmlDocument xml = new XmlDocument();
                xml.Load("settings.xml");
    
                XmlElement newcatalogentry = xml.CreateElement("settings");
    
                XmlElement firstelement = xml.CreateElement("AlarmFileLocation");
                firstelement.InnerText = "path";
                newcatalogentry.AppendChild(firstelement);
    
                XmlElement secondelement = xml.CreateElement("AutoLogin");
                secondelement.InnerText = "ON";
                newcatalogentry.AppendChild(secondelement);
    
                xml.DocumentElement.InsertAfter(newcatalogentry, xml.DocumentElement.LastChild);
                xml.Save("settings.xml");
    Modify XML data :
    <><><><><><><>
    Steps :
    1- choose the xml file location and load it
    2- Create a list of Nodes
    3- loop inside it to get values of elements
    4- choose a certain element and change it`s innertext
    5- save the file

    And here is the code :
    Code:
                                   
                XmlDocument xml1 = new XmlDocument();
    
                xml1.Load("settings.xml");
    
                XmlNodeList nodes = xml1.SelectNodes("//settings");
                foreach (XmlElement element in nodes)
                {
                    element.SelectSingleNode("AlarmFileLocation").InnerText = "Modified";
                    element.SelectSingleNode("AutoLogin").InnerText = "OFF";
                    xml1.Save("settings.xml");
                }
    That will modify elements AlarmFileLocation and AutoLogin innertext`s to "Modified" and "OFF"

    Delete XML Childs
    <><><><><><><>
    Steps :
    1- choose the xml file location and load it
    2- Create a list of Nodes
    3- loop inside it and delete each element

    Here is the Code :
    Code:
                XmlDocument xml1 = new XmlDocument();
    
                xml1.Load("settings.xml");
    
                XmlNodeList nodes = xml1.SelectNodes("//settings");
                foreach (XmlElement element in nodes)
                {
                    element.RemoveAll();
                }
                xml1.Save("settings.xml");
    The final output of the XML file after deleting :
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    < settings>
    < /settings>

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts