HtmlTextWriter writer = new HtmlTextWriter(File.CreateText("example.html")); writer.RenderBeginTag(HtmlTextWriterTag.Div); writer.AddAttribute("class", "example-class"); writer.RenderEndTag(); writer.Close();
HtmlTextWriter writer = new HtmlTextWriter(File.CreateText("example.html")); writer.AddAttribute(HtmlTextWriterAttribute.Src, "example.png"); writer.AddAttribute("style", "width: 50px; height: 50px;"); writer.RenderBeginTag(HtmlTextWriterTag.Img); writer.RenderEndTag(); writer.Close();In this example, the AddAttribute method is used to add a source and style attribute to an img element. The source attribute is added using the HtmlTextWriterAttribute enum, and the style attribute is added as a simple string. The HtmlTextWriter class is included in the System.Web.UI.WebControls namespace.