Monday, 26 November 2018

Convert HTML to Plain Text Dynamics AX 2012

Hello folks,

When I hear from one of my friend, that he needs to convert HTML text to Plain Text. I thought let me checkout this on Microsoft dynamics AX. I start searching out and there is no straightforward way to do this in Dynamics AX (No class or method to do this) then I decide to make my own.

Here is a small piece of code which going to help you out to convert you HTML text to Text. This can be your email text, which need to stop in text file or could be to show on any field.


Comment below if this saved your time and helped.

4 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. if you need to survive

    static public str Html2Text(str _htmlStrFormat)
    {
    System.Text.RegularExpressions.Regex regex,lineBreakRegex,stripFormattingRegex,tagWhiteSpaceRegex;
    System.Text.RegularExpressions.RegexOptions regexOptions;
    str text = '';
    str tagWhiteSpace,stripFormatting,lineBreak;
    str htmlStrFormat = _htmlStrFormat;

    tagWhiteSpace = @"(>|$)(\W|\n|\r)+<";
    stripFormatting = @"<[^>]*(>|$)";
    lineBreak = @"<(br|BR)\s{0,1}\/{0,1}>";
    lineBreakRegex = new System.Text.RegularExpressions.Regex(lineBreak,System.Text.RegularExpressions.RegexOptions::Multiline);
    stripFormattingRegex = new System.Text.RegularExpressions.Regex(stripFormatting,System.Text.RegularExpressions.RegexOptions::Multiline);
    tagWhiteSpaceRegex = new System.Text.RegularExpressions.Regex(tagWhiteSpace,System.Text.RegularExpressions.RegexOptions::Multiline);

    text = htmlStrFormat;

    text = System.Net.WebUtility::HtmlDecode(text);
    text = tagWhiteSpaceRegex.Replace(text,"><");
    text = lineBreakRegex.Replace(text,"\n");
    text = stripFormattingRegex.Replace(text,'');

    return text;
    }

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. It was a great information and Its really worth reading it.
    D365 AX Online Training

    ReplyDelete