在ToString
我的实体的方法中,我尝试生成一个包含引号的信息字符串。所以我在引用之前使用反斜杠,一切都很好,直到我尝试将返回值分配给字符串变量,然后所有反斜杠回归。这里是 ToString 函数:
public override string ToString()
{
string content = "{";
content += "\"serviceType\":" + ServiceType.Name + ",";
content += "\"Debt\":" + amount;
content += "}";
return content;
}
之后,我尝试使用StringWriter
并HtmlTextWriter
生成 HTML,但是在将返回值分配给字符串变量之后,变量中有很多\
, \r
, \n
, \t
。这里是函数:
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();
}
并将值分配给变量后:
<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>
这里发生了什么事?