create xml file from c# code example
Example 1: how to create xml file in c#
new XDocument(
new XElement("root",
new XElement("someNode", "someValue")
)
)
.Save("foo.xml");
Example 2: how to create xml file in c#
private XmlElement CreateLicenseKeyValuePairNodes(XmlDocument document)
{
XmlElement keyValueElement = document.CreateElement("KeyValuePair");
XmlElement keyElement = document.CreateElement("Key");
XmlElement valueElement = document.CreateElement("Value");
keyValueElement.AppendChild(keyElement);
keyValueElement.AppendChild(valueElement);
return keyValueElement;
}
protected virtual void CreateXmlDoc(int count)
{
XmlDocument outputDocument = new XmlDocument();
XmlElement rootElement = outputDocument.CreateElement("Base");
XmlElement customerElement = outputDocument.CreateElement("Customer");
XmlElement projectElement = outputDocument.CreateElement("Project");
XmlElement programIdElement = outputDocument.CreateElement("ProgramID");
XmlElement activationIdElement = outputDocument.CreateElement("ActivationID");
XmlElement featureElement = outputDocument.CreateElement("Features");
XmlElement releaseElement = outputDocument.CreateElement("ReleaseDate");
XmlElement validationElement = outputDocument.CreateElement("ValidUntil");
rootElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
rootElement.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
rootElement.AppendChild(customerElement);
rootElement.AppendChild(projectElement);
rootElement.AppendChild(programIdElement);
rootElement.AppendChild(activationIdElement);
rootElement.AppendChild(featureElement);
for (int i = 0; i < count; i++)
{
featureElement.AppendChild(CreateLicenseKeyValuePairNodes(outputDocument));
}
rootElement.AppendChild(releaseElement);
rootElement.AppendChild(validationElement);
outputDocument.AppendChild(rootElement);
outputDocument.Save(@".\Template.xml");
}