将写代码过程中经常用到的一些代码段备份一次,下面代码段是关于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);
}