class MainClass
{
XmlDocument xmldoc;
XmlNode xmlnode;
XmlElement xmlelem;
XmlElement xmlelem2;
XmlText xmltext;
public SaveXml()
{
xmldoc = new XmlDocument();
//加入XML的声明段落
xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
xmldoc.AppendChild(xmlnode);
//加入一个根元素
xmlelem = xmldoc.CreateElement("", "ROOT", "");
xmltext = xmldoc.CreateTextNode("Root Text");
xmlelem.AppendChild(xmltext);
xmldoc.AppendChild(xmlelem);
//加入另外一个元素
xmlelem2 = xmldoc.CreateElement("SampleElement");
xmlelem2 = xmldoc.CreateElement("", "SampleElement", "");
xmltext = xmldoc.CreateTextNode("The text of the sample element");
xmlelem2.AppendChild(xmltext);
xmldoc.ChildNodes.Item(1).AppendChild(xmlelem2);
//保存创建好的XML文档
try
{
xmldoc.Save("d:\\data1.xml");
}
catch (Exception e)
{
//显示错误信息
Console.WriteLine(e.Message);
}
//Console.ReadLine ( ) ;
}
}
}