本文实例讲述了C#简单写入xml文件的方法。分享给大家供大家参考。具体实现方法如下:
static void writeTree(XmlNode xmlElement, int level) { String levelDepth = ""; for(int i=0;i<level;i++) { levelDepth += " "; } Console.Write("n{0}<{1}",levelDepth,xmlElement.Name); XmlAttributeCollection xmlAttributeCollection = xmlElement.Attributes; foreach(XmlAttribute x in xmlAttributeCollection) { Console.Write(" {0}='{1}'",x.Name,x.Value); } Console.Write(">"); XmlNodeList xmlNodeList = xmlElement.ChildNodes; ++level; foreach(XmlNode x in xmlNodeList) { if(x.NodeType == XmlNodeType.Element) { writeTree((XmlNode)x, level); } else if(x.NodeType == XmlNodeType.Text) { Console.Write("n{0} {1}",levelDepth,(x.Value).Trim()); } } Console.Write("n{0}</{1}>",levelDepth,xmlElement.Name); }
希望本文所述对大家的C#程序设计有所帮助。