There are several ways interact with an Xml file.

  1. Xml Document
  2. XDocument
  3. 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 and XDocument class 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