XmlDocument xmlDoc = new XmlDocument();
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
// Create the root element
XmlElement rootNode = xmlDoc.CreateElement("kml");
rootNode.SetAttribute("xmlns", @"http://earth.google.com/kml/2.1");
xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
xmlDoc.AppendChild(rootNode);
// Add the Document
XmlElement documentNode = CreateElement(xmlDoc, "Document", "Test KML File");
rootNode.AppendChild(documentNode);
xmlDoc.Save(outputFilePath + FileName + ".kml");
public XmlElement CreateElement(XmlDocument xmlDoc, string elementType, string name)
{
XmlElement documentNode = xmlDoc.CreateElement(elementType);
XmlElement documentNameNode = xmlDoc.CreateElement("name");
XmlText nameText = xmlDoc.CreateTextNode(name);
documentNode.AppendChild(documentNameNode);
documentNameNode.AppendChild(nameText);
return documentNode;
}