Reputation: 1729
I am reading an XML file and I want to write the actual contents to HTML. I think the problem is that HtmlTextWriter is treating my XML tags as HTML tags. I have tried using HttpUtility.HtmlEncode/Decode and SecurityElement.Escape but these don't work. How do I escape the tags so that I get literal strings to write to my HTML page.
private string WriteXmlToHtml()
{
StringWriter stringWriter = new StringWriter();
using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
{
int counter = 0;
string line;
StreamReader file = new StreamReader("test.xml");
while ((line = file.ReadLine()) != null)
{
//HttpUtility.HtmlDecode(line);
//System.Security.SecurityElement.Escape
writer.Write(line);
counter++;
}
}
return stringWriter.ToString();
}
Upvotes: 0
Views: 709
Reputation: 23937
//edit: I have shamelessy implemented Xanatos' <pre>
tags into the output string. Source in the comments
writer.Write(String.Format("<pre>{0}</pre>",HttpUtility.HtmlEncode(line)));
produces the following output:
<?xml version="1.0" encoding="utf-8"?>
<Settings>
<Code>D:picture\simple</Code>...
plus it adds a line break after each line. Unfortunately it does not keep the indention.
This should get rendered as valid xml in your html file.
Upvotes: 1
Reputation: 2361
Try using this:
private string WriteXmlToHtml()
{
string filePath = @"d:\abc.xml";
return Server.HtmlEncode(File.ReadAllText(filePath));
}
Upvotes: 0