Writing XML Fragments
کد:
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);
}