I'm looking for code or a library in C# that convert HTML email format to plain text format. I want to send my emails with both format.
Also, I would like the href links to be converted to text as well. Exactly how mailchimp does it here: https://templates.mailchimp.com/resources/html-to-text/
Any idea?
You can use HtmlAgilityPack and loop through nodes to extract text. Example: Grab all text from html with Html Agility Pack
Related
Is there a way to convert a html string for example content saved using a wysiwyg editor such as tinyMCE to formatted plain text i.e. retain line breaks.
I've been looking at the HTML Agility Pack but there are no examples that show how you can achieve something like the above.
Basically I want send content saved using tinyMCE in an email notification but i need to ensure any styles and formatting saved by tinyMCE don't break the styles in my HTML email hence the need to convert the content to plain text.
Any examples would be appreciated.
How would i show the output as HTML. I have tried HTML Decode and it still didn't work.
#section Grid {
#Server.HtmlDecode(lister.gen(new System.IO.StreamReader(Server.MapPath("~/Grid.xml")).ReadToEnd()))
}
Edit: I am taking XML from output.InnerXml (A XMLDocument) and trying to put it into a HTML Document as HTML (As in <a> is a link and <img> is a picture and not Text)
It turns out I had to add #Html.Raw along with HtmlDecode for it to display correctly
#Html.Raw(Server.HtmlDecode(lister.gen(new System.IO.StreamReader(Server.MapPath("~/Grid.xml")).ReadToEnd())))
If you want to show the HTML in an HTML page, you need to use HTML encode, not decode. This will put the proper tags in to turn < and > (and other HTML elements.
ADDED due to comment:
If showing XML AS HTML is the goal, then you will end up extracting the XML, from the DOM and putting it into the format you desire. You can bind XML to a table if you are simply trying to get it into a grid. If you need sorting, etc, LINQ to XML works nicely.
ADDED - second edit
Your XML appears to be XHTML, so you can simply throw it into the stream at the correct location. I would create a server control and then Response.Write the XML from the server control. You will want the decoded version based on what you posted. I was assuming you wanted to show the XML in the page, which was incorrect.
There is one minor bit of an issue with the XML, as it contains paragraph and div tags inside of an anchor tag. Not illegal, but not necessary in this case.
I have a HTML page with 2 lines. The 2 lines are regarding an update ,but this update can be downloaded only by a specific user.
Basically the HTML file contains:
beast
1.11
the 1st line is the user and the 2nd is the version.There are no HTML headers or other lines.
Now my problem is,that I really have no idea how to read just the 2nd line
A text document can be easily manipulated by:
File.ReadLines("Text.txt").ElementAt(1);
Is there a similar command wich can be used for reading an HTML file line by line?
HUGE thanks for any reply!!!!
You can use that same exact code to read HTML.
HTML files are ordinary text files that happen to contain HTML tags.
If you want to parse the HTML tags, use HTML Agility Pack (on NuGet).
I have a PDF Form and I'm trying to put HTML formatted text into a field using itextsharp 5.1.3 using c#. I want the text to keep the same formatting as it has in the HTML. How can I do this?
I've searched everywhere and I haven't been able to find anything. The only examples I can find show how to convert an entire document, but none show a clear way to convert a string of HTML code to PDF format and set a Field with it.
After scrubbing my field with AntiXss.HtmlEncode is there a way to remove all the html elements because they still show up as literals in the display?
No, there isn't. If you want to remove HTML elements and then HtmlEncode the result, you have to actually parse the code and remove the elements.
You can use the HTML Agility Pack or any other HTML parser to parse the content, get the InnerText of the root element (this will be only the text of the content) and then call AntiXss.HtmlEncode on that result.