1

I am currently loading a html file from a filepath and reading it as text. I then replace certain characters in the file itself and I want to convert it back to html.

This is how I do it currently:

HtmlDocument document = new HtmlDocument();
document.Load(@message.Location);
content = document.DocumentNode.OuterHtml;

//Code to replace text

var eContent = HttpUtility.HtmlEncode(content);

When I debug and check what eContent holds, I can see newline characters like "\r\n". If I copy and paste the text into a .html file, only the text appears, not a proper html page.

I'm using Html AgilityPack already and am unsure of what else I need to do.

EDIT:

I have also tried

var result = new HtmlString(content);
Madushan
  • 6,977
  • 31
  • 79
JianYA
  • 2,750
  • 8
  • 60
  • 136

3 Answers3

1

HtmlAgilityPack is great to read and modify Html files you cannot create readable output.

Try with this

Mrpit
  • 101
  • 1
  • 7
0

I have done this before using...

 string savePath = "path to save html file, ie C://myfile.html";

 string textRead = File.ReadAllText(@"Path of original html file");
  //replace or manipulate as needed... ie textRead = textRead.Replace("", "");
 File.WriteAllText(savePath, textRead);
Daniel Jee
  • 51
  • 5
  • Yeah but I don't want to write it to a file. I need the string to send via another function. – JianYA Jul 16 '19 at 12:59
  • then remove the File.WriteAllText and pass the string textRead where ever you want to, it will keep all its tags and HTML format. – Daniel Jee Jul 17 '19 at 07:40
0

Try to use ContentResult, which inherits ActionResult. Just remember to set ContentType to text/html.

[HttpGet]
public IActionResult FileToTextToHtml()
    {
        string fileContents = System.IO.File.ReadAllText("D:\\HtmlTest.html");

        var result= new ContentResult()
        {
            Content = fileContents,
            ContentType = "text/html",
        };
        return result;
    }
Xueli Chen
  • 11,987
  • 3
  • 25
  • 36