# XmlDocument and the System.Xml namespace

# XmlDocument vs XDocument (Example and comparison)

There are several ways interact with an Xml file.

    • Xml Document
    • XDocument
    • XmlReader/XmlWriter

      Before LINQ to XML we were used XMLDocument for manipulations in XML like adding attributes, elements and so on. Now LINQ to XML uses XDocument for the same kind of thing. Syntaxes are much easier than XMLDocument and it requires a minimal amount of code.

      Also XDocument is mutch faster as XmlDocument. XmlDoucument is an old and dirty solution for query an XML document.

      I am going to show some examples of XmlDocument class (opens new window) and XDocument class (opens new window) class:

    Load XML file

    string filename = @"C:\temp\test.xml";


    XmlDocument _doc = new XmlDocument();


    XDocument _doc = XDocument.Load(fileName);

    Create XmlDocument


    XmlDocument doc = new XmlDocument();
    XmlElement root = doc.CreateElement("root");
    root.SetAttribute("name", "value");
    XmlElement child = doc.CreateElement("child");
    child.InnerText = "text node";


    XDocument doc = new XDocument(
        new XElement("Root", new XAttribute("name", "value"), 
        new XElement("Child", "text node"))
    <root name="value">

    Change InnerText of node in XML


    XmlNode node = _doc.SelectSingleNode("xmlRootNode");
    node.InnerText = value;


    XElement rootNote = _doc.XPathSelectElement("xmlRootNode"); 
    rootNode.Value = "New Value";

    Save File after edit

    Make sure to safe the xml after any change.

    // Safe XmlDocument and XDocument

    Retreive Values from XML


    XmlNode node = _doc.SelectSingleNode("xmlRootNode/levelOneChildNode");
    string text = node.InnerText;


    XElement node = _doc.XPathSelectElement("xmlRootNode/levelOneChildNode");
     string text = node.Value;

    Retreive value from all from all child elements where attribute = something.


    List<string> valueList = new List<string>(); 
        foreach (XmlNode n in nodelist)
            if(n.Attributes["type"].InnerText == "City")


    var accounts = _doc.XPathSelectElements("/data/summary/account").Where(c => c.Attribute("type").Value == "setting").Select(c => c.Value);

    Append a node


    XmlNode nodeToAppend = doc.CreateElement("SecondLevelNode");
    nodeToAppend.InnerText = "This title is created by code";
    /* Append node to parent */
    XmlNode firstNode= _doc.SelectSingleNode("xmlRootNode/levelOneChildNode");
    /*After a change make sure to safe the document*/


    _doc.XPathSelectElement("ServerManagerSettings/TcpSocket").Add(new XElement("SecondLevelNode"));
     /*After a change make sure to safe the document*/

    # Reading from XML document

    An example XML file

            <One number="12"/>
            <Two number="14"/>
            <One number="14"/>
            <Two number="16"/>

    Reading from this XML file:

       using System.Xml;
        using System.Collections.Generic;
        public static void Main(string fullpath)
            var xmldoc = new XmlDocument();
            var oneValues = new List<string>();
            // Getting all XML nodes with the tag name
            var accountNodes = xmldoc.GetElementsByTagName("Account");
            for (var i = 0; i < accountNodes.Count; i++)
                // Use Xpath to find a node
                var account = accountNodes[i].SelectSingleNode("./One");
                if (account != null && account.Attributes != null)
                    // Read node attribute

    # Basic XML document interaction

    public static void Main()
        var xml  = new XmlDocument();
        var root = xml.CreateElement("element");
            // Creates an attribute, so the element will now be "<element attribute='value' />"
            root.SetAttribute("attribute", "value");
        // All XML documents must have one, and only one, root element        
        // Adding data to an XML document
        foreach (var dayOfWeek in Enum.GetNames((typeof(DayOfWeek))))
            var day = xml.CreateElement("dayOfWeek");
                day.SetAttribute("name", dayOfWeek);
            // Don't forget to add the new value to the current document!
        // Looking for data using XPath; BEWARE, this is case-sensitive
        var monday = xml.SelectSingleNode("//dayOfWeek[@name='Monday']");
        if (monday != null)
            // Once you got a reference to a particular node, you can delete it
            // by navigating through its parent node and asking for removal
        // Displays the XML document in the screen; optionally can be saved to a file