using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace DemoClass
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
XmlDocument document = new XmlDocument();
document.Load("Products.xml");
Textbox1.Text = FormatTextArea(document.DocumentElement as XmlNode, "", "");
}
private string FormatTextArea(XmlNode node, string textMark, string indent)
{
if (node is XmlText)
{
textMark += node.Value;
return textMark;
}
if (string.IsNullOrEmpty(indent))
indent = "";
else
{
textMark += "\r\n" + indent;
}
if (node is XmlComment)
{
textMark += node.OuterXml;
return textMark;
}
textMark += "<" + node.Name;
if (node.Attributes.Count > 0)
{
AddAttributes(node, ref textMark);
}
if (node.HasChildNodes)
{
textMark += ">";
foreach (XmlNode child in node.ChildNodes)
{
textMark = FormatText(child, textMark, indent + " ");
}
if (node.ChildNodes.Count == 1 &&
(node.FirstChild is XmlText || node.FirstChild is XmlComment))
textMark += "</" + node.Name + ">";
else
textMark += "\r\n" + indent + "</" + node.Name + ">";
}
else
textMark += " />";
return textMark;
}
private void AddAttributes(XmlNode node, ref string textref)
{
foreach (XmlAttribute xaAttr in node.Attributes)
{
textref += " " + xaAttr.Name + "='" + xaAttr.Value + "'";
}
}
}
}