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

I am using an XElement object to generate some HTML in the code on an ASP.NET page.

I can or cannot add some XAttributes to this XElement when I go, as follows:

var elmnt = new XElement("div",
     new XAttribute("id", "myDiv"),
     );

Now, if I want to add some content to myDiv that contains HTML, XElement automatically eludes this, which is undesirable in my situation.

So, if I have:

var elmnt = new XElement("div",
     new XAttribute("id", "myDiv"),
     "<span id='content'>hello world</span>"
     );

And then I pass it to the Placeholder object using the following code:

myPlaceholder.Controls.Add(new System.Web.UI.WebControls.Literal { Text = elmnt.CreateNavigator().OuterXml });

When the page loads, the source indicates that the internal content inside elmnt has been escaped and has the following format in the page source:

&lt;span id='content'&gt;hello world&lt;/span&gt;

, XML, , HTML, HTML, XElement, ? ?

+5
4
 var elmnt = new XElement("div",
   new XAttribute("id", "myDiv"),
   new XRaw("<span id='content'>hello world</span>")
 );


public class XRaw : XText
{
  public XRaw(string text):base(text){}
  public XRaw(XText text): base(text){}

  public override void WriteTo(System.Xml.XmlWriter writer)
  {
    writer.WriteRaw(this.Value);
  }
}
+7

HTML XML, , , XML

, xmlelement (span), ( , content), "hello world". xmlelement .

, , . "" , : asp:label aps:placeholder, html . .

0

, HtmlAgilitypack, HTML .

HTML XElement.

, , , , .

0

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(); //<span id='content'>hello world</span>
-1

All Articles