jeudi 2 avril 2015

Displaying XML Files to RichTextBox with indentations C# Winforms

I'm writing a program so it will read a xml file and output back in a richtextbox, is there a way to read the file with indents?


Here is how I display it in richtextbox:



if (txtFileDestination != String.Empty)
{
XmlReader reader = XmlReader.Create(txtFileDestination);

while (reader.Read())
{
switch (reader.NodeType)
{
//check each nodetype if exist
case XmlNodeType.Element:
//check attributes if exist
if (reader.HasAttributes)
{
richTextBox1.AppendText("<" + reader.Name + " ");
while (reader.MoveToNextAttribute())
{
richTextBox1.AppendText(reader.Name + " =\"" + reader.Value + "\" ");
}
richTextBox1.AppendText(">\n");

}
else
{
richTextBox1.AppendText("<" + reader.Name + ">\n");
}

reader.MoveToElement();

break;

case XmlNodeType.Text:
richTextBox1.AppendText(reader.Value + "\n");
break;
case XmlNodeType.CDATA:
richTextBox1.AppendText("<![CDATA[" + reader.Value + "]]>\n");
break;
case XmlNodeType.ProcessingInstruction:
richTextBox1.AppendText("<?" + reader.Name + " " + reader.Value + "?>\n");
break;
case XmlNodeType.Comment:
richTextBox1.AppendText("<!--" + reader.Value + "-->\n");
break;
case XmlNodeType.XmlDeclaration:
richTextBox1.AppendText("<?" + reader.Value + "?>\n");
break;
case XmlNodeType.Document:
break;
case XmlNodeType.DocumentType:
richTextBox1.AppendText("<!DOCTYPE " + reader.Name + " [" + reader.Value + "]\n");
break;
case XmlNodeType.EntityReference:
richTextBox1.AppendText(reader.Name + "\n");
break;
case XmlNodeType.EndElement:
richTextBox1.AppendText("</" + reader.Name + ">\n");
break;
}
}
}

Aucun commentaire:

Enregistrer un commentaire