How to avoid System.Xml.Linq.XElement escaping HTML content?

前端 未结 4 1225
孤城傲影
孤城傲影 2020-12-18 03:36

I\'m using the XElement object to build some HTML in the code-behind on an ASP.NET page.

I may or may not add some XAttributes to this XElement as I go along, in the

4条回答
  •  一整个雨季
    2020-12-18 04:23

    Why wouldn't you use HtmlTextWriter?

    StringWriter stringWriter = new StringWriter();
    
    HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
    htmlWriter.RenderBeginTag(HtmlTextWriterTag.Span);
    htmlWriter.AddAttribute(HtmlTextWriterAttribute.Id, "content");
    htmlWriter.Write("hello world");
    htmlWriter.RenderEndTag();
    
    htmlWriter.Flush();
    
    stringWriter.ToString(); //hello world
    

提交回复
热议问题