using System;
using System.Xml;
namespace XmlBuilderCS
{
class Program
{
void CloneSomeNodes(XmlDocument xmlDoc)
{
XmlNode oldNode =
xmlDoc.GetElementsByTagName("node1").Item(0);
for (int i = 0; i < 5; i++)
{
XmlNode newNode = oldNode.CloneNode(true);
xmlDoc.DocumentElement.AppendChild(
xmlDoc.CreateTextNode("\n\t"));
xmlDoc.DocumentElement.AppendChild(
newNode);
}
}
void BuildAndPrintXml()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
// Create a processing instruction element.
XmlProcessingInstruction pi =
xmlDoc.CreateProcessingInstruction(
"xml", "version='1.0'");
xmlDoc.AppendChild(pi);
// Create a comment element.
XmlComment comment =
xmlDoc.CreateComment(
"sample xml file created using XML DOM object.");
xmlDoc.AppendChild(comment);
// Create the root element.
XmlElement root =
xmlDoc.CreateElement("root");
// Create a "created" attribute for the <root> element, and
// assign the "using dom" character data as the attribute value.
XmlAttribute attr =
xmlDoc.CreateAttribute("created");
attr.Value = "using dom";
root.SetAttributeNode(attr);
xmlDoc.AppendChild(root);
// Next, we will create and add three nodes to the <root> element.
string newline = "\n";
string newlineTab = "\n\t";
string newlineTabTab = "\n\t\t";
// Add NEWLINE+TAB for identation before <node1>.
root.AppendChild(
xmlDoc.CreateTextNode(newlineTab));
// Create a <node1> to hold text content.
XmlElement element = xmlDoc.CreateElement("node1");
element.InnerText = "some character data";
// Append <node1> to <root>.
root.AppendChild(element);
// Add NEWLINE+TAB for identation before <node2>.
root.AppendChild(
xmlDoc.CreateTextNode(newlineTab));
// Create a <node2> to hold a CDATA section.
element = xmlDoc.CreateElement("node2");
XmlCDataSection cdata =
xmlDoc.CreateCDataSection("<some mark-up text>");
element.AppendChild(cdata);
// Append <node2> to <root>.
root.AppendChild(element);
// Add NEWLINE+TAB for identation before <node3>.
root.AppendChild(
xmlDoc.CreateTextNode(newlineTab));
// Create <node3> to hold a doc fragment with three sub-elements.
element = xmlDoc.CreateElement("node3");
// Create a document fragment to hold three sub-elements.
XmlDocumentFragment fragment =
xmlDoc.CreateDocumentFragment();
// Add NEWLINE+TAB+TAB for identation before <subnode1>.
fragment.AppendChild(
xmlDoc.CreateTextNode(newlineTabTab));
// Create and append <subnode1>.
fragment.AppendChild(
xmlDoc.CreateElement("subnode1"));
// Add NEWLINE+TAB+TAB for identation before <subnode2>.
fragment.AppendChild(
xmlDoc.CreateTextNode(newlineTabTab));
// Create and append <subnode2>.
fragment.AppendChild(
xmlDoc.CreateElement("subnode2"));
// Add NEWLINE+TAB+TAB for identation before <subnode3>.
fragment.AppendChild(
xmlDoc.CreateTextNode(newlineTabTab));
// Create and append <subnode3>.
fragment.AppendChild(
xmlDoc.CreateElement("subnode3"));
// Add NEWLINE+TAB after </subnode> in fragment.
fragment.AppendChild(
xmlDoc.CreateTextNode(newlineTab));
// Append fragment to <node3> (element).
element.AppendChild(fragment);
// Append <node3> to <root>.
root.AppendChild(element);
// Add NEWLINE for identation before </root>.
root.AppendChild(xmlDoc.CreateTextNode(newline));
Console.WriteLine(
"Dynamically created DOM:\n{0}\n", xmlDoc.OuterXml);
CloneSomeNodes(xmlDoc);
Console.WriteLine(
"After cloning some nodes:\n{0}\n", xmlDoc.OuterXml);
}
static void Main(string[] args)
{
try
{
new Program().BuildAndPrintXml();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("\nDone\n");
Console.ReadLine();
}
}
}