I've covered in previous blogs how to manipulate XML using the XmlDocument class and how to manipulate XML using LINQ to XML. They use the XmlDocument class and the XDocument class respectively. In this article, I will introduce another class, XmlTextWriter. We use these three classes to write the same XML content into the document to see which writing method is the most intuitive and simple.
The XML document we want to write is
<?xml version="1.0" encoding="UTF-8"?>
<Contacts>
<Contact >
<Name>Daisy Abbey</Name>
<Gender>female</Gender>
</Contact>
</Contacts>
(1) Use XmlDocument class:
var xmlDoc = new XmlDocument();
//Create the xml declaration first
(("1.0", "utf-8", null));
//Create the root node and append into doc
var el = ("Contacts");
(el);
// Contact
XmlElement elementContact = ("Contact");
XmlAttribute attrID = ("id");
= "01";
(attrID);
(elementContact);
// Contact Name
XmlElement elementName = ("Name");
= "Daisy Abbey";
(elementName);
// Contact Gender
XmlElement elementGender = ("Gender");
= "female";
(elementGender);
("");
(2) XDocument class using LINQ to XML:
var doc = new XDocument(
new XElement("Contacts",
new XElement("Contact",
new XAttribute("id", "01"),
new XElement("Name", "Daisy Abbey"),
new XElement("Gender", "female")
)
)
);
("");
(3) Use XmlTextWriter class:
String filename = ("");
using (StreamWriter sw = new StreamWriter(filename))
{
// Create Xml Writer.
XmlTextWriter xmlWriter = new XmlTextWriter(sw);
// You can also use public XmlTextWriter(string filename, Encoding encoding) to construct
// encoding defaults to UTF-8.
//XmlTextWriter writer = new XmlTextWriter("", null);
// Set indenting so that its easier to read XML when open in Notepad and such apps.
= ;
// This will output the XML declaration
();
("Contacts");
("Contact");
("id", "01");
("Name", "Daisy Abbey");
("Gender", "female");
// close contact </contact>
();
// close contacts </contact>
();
();
();
}
From the above code, we can basically see that using LINQ to XML is the easiest.
The XML document we want to write is
Copy the codeThe code is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<Contacts>
<Contact >
<Name>Daisy Abbey</Name>
<Gender>female</Gender>
</Contact>
</Contacts>
(1) Use XmlDocument class:
Copy the codeThe code is as follows:
var xmlDoc = new XmlDocument();
//Create the xml declaration first
(("1.0", "utf-8", null));
//Create the root node and append into doc
var el = ("Contacts");
(el);
// Contact
XmlElement elementContact = ("Contact");
XmlAttribute attrID = ("id");
= "01";
(attrID);
(elementContact);
// Contact Name
XmlElement elementName = ("Name");
= "Daisy Abbey";
(elementName);
// Contact Gender
XmlElement elementGender = ("Gender");
= "female";
(elementGender);
("");
(2) XDocument class using LINQ to XML:
Copy the codeThe code is as follows:
var doc = new XDocument(
new XElement("Contacts",
new XElement("Contact",
new XAttribute("id", "01"),
new XElement("Name", "Daisy Abbey"),
new XElement("Gender", "female")
)
)
);
("");
(3) Use XmlTextWriter class:
Copy the codeThe code is as follows:
String filename = ("");
using (StreamWriter sw = new StreamWriter(filename))
{
// Create Xml Writer.
XmlTextWriter xmlWriter = new XmlTextWriter(sw);
// You can also use public XmlTextWriter(string filename, Encoding encoding) to construct
// encoding defaults to UTF-8.
//XmlTextWriter writer = new XmlTextWriter("", null);
// Set indenting so that its easier to read XML when open in Notepad and such apps.
= ;
// This will output the XML declaration
();
("Contacts");
("Contact");
("id", "01");
("Name", "Daisy Abbey");
("Gender", "female");
// close contact </contact>
();
// close contacts </contact>
();
();
();
}
From the above code, we can basically see that using LINQ to XML is the easiest.