Backslash using for escaping qute regenerate after asigning String

270 Views Asked by At

in ToString methode of my Entity, i try to generate a informational string includes quotes. so i use backslash before quote and everything is good until i try to assign return value to a string variable and after that all backslash comeback .here is the ToString function:

public override string ToString()
    {
        string content = "{";
        content += "\"serviceType\":" + ServiceType.Name + ",";
        content += "\"Debt\":" + amount;
        content += "}";
        return content;
    }

after that i try to use StringWriter and HtmlTextWriter to generate a HTML but after assign the return value to a string variable there is a lot of \, \r, \n, \tin variable.here is the function:

    public string ToHtmlString()
    {

        StringWriter stringWriter = new StringWriter();
        using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
        {
            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%");
            writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, "1px solid black");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Direction, "rtl");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);// Begin Table

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("زیر خدمت");
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(ServiceType.Name);
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("مبلغ");
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(amount);
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.RenderEndTag();// End Table
        }
        return stringWriter.ToString();
    }

and after assigning the value to a variable:

<table style=\"width:100%;border-style:1px solid black;direction:rtl;\">\r\n\t<tr>\r\n\t\t<td>زیر خدمت</td><td>درمان</td>\r\n\t</tr><tr>\r\n\t\t<td>مبلغ</td><td>6000</td>\r\n\t</tr>\r\n</table>

what's happening here?

1

There are 1 best solutions below

4
On BEST ANSWER

That result seems ok to use in a webBrowser control inside C#. If you prefer to avoid these newlines and tabs (for a real browser), you can try replacing a few lines of your code like this.

OLD CODE:

using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
    {

NEW CODE:

using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter, string.Empty))
    {
        writer.NewLine = string.Empty;

The second parameter in the HtmlTextWriter is the string used as tab. So the result you get would be like:

<table style=\"width:100%;border-style:1px solid black;direction:rtl;\"><tr><td>زیر خدمت</td><td>درمان</td></tr><tr><td>مبلغ</td><td>6000</td></tr></table>

EDIT: In order to avoid the \" you could use <? php echo or <%=(for asp), or better rework the result as:

return stringWriter.ToString().Replace("\"", "'");