Protected void btnCreate_Click (object sender, EventArgs e)
{
// Define XMLDocument
XmlDocument xmlDocument = new XmlDocument ();
// Define the XML document header file
XmlDeclaration xmlDeclaration = xmlDocument. CreateXmlDeclaration ("1.0", "UTF-8", null );
// Add the XML Document Header
XmlDocument. AppendChild (xmlDeclaration );
// Define the XML Root
XmlElement xmlRoot = xmlDocument. CreateElement ("Roots ");
// Add the XML Root
XmlDocument. AppendChild (xmlRoot );
// Add the root attribute
XmlRoot. SetAttribute ("RootAttribute", "Value ");
// Modify the root Attribute Value
XmlRoot. GetAttributeNode ("RootAttribute"). Value = "FixValue ";
// Define a node
XmlNode xmlElement;
// Create nodes cyclically
For (int I = 0; I <2; I ++)
{
// Create an XML Root Node
XmlElement = xmlDocument. CreateElement ("Element ");
// List of attributes required by XML
Foreach (KeyValuePair <String, String> keyValuePair in NameValueDictionary ())
{
// Define attributes in the XML Root Node
XmlAttribute oneAttribute = xmlDocument. CreateAttribute (keyValuePair. Key );
OneAttribute. Value = keyValuePair. Value;
XmlAttribute secAttribute = xmlDocument. CreateAttribute (keyValuePair. Key );
SecAttribute. Value = keyValuePair. Value;
// Add attributes in the XML Root Node
XmlElement. Attributes. Append (oneAttribute );
XmlElement. Attributes. Append (secattriend );
// Add the XML Root Node
XmlRoot. AppendChild (xmlElement );
}
}
// Save the XML document
XmlDocument. Save (Server. MapPath ("OutDocument. XML "));
}
// List of attributes required by XML
Private Dictionary <String, String> NameValueDictionary ()
{
Dictionary <String, String> nameValueDictionary = new Dictionary <String, String> ();
NameValueDictionary. Add ("AttributeOne", "One ");
NameValueDictionary. Add ("AttributeSec", "Second ");
Return nameValueDictionary;
}
Generated XML Module
<? Xml version = "1.0" encoding = "UTF-8"?>
<Roots RootAttribute = "FixValue">
<Element AttributeOne = "One" AttributeSec = "Second"/>
<Element AttributeOne = "One" AttributeSec = "Second"/>
</Roots>