XhtmlTextWriter Class

Writes Extensible Hypertext Markup Language (XHTML)-specific characters, including all variations of XHTML modules that derive from XTHML, to the output stream for an ASP.NET server control for mobile devices. Override the XhtmlTextWriter class to provide custom XHTML rendering for ASP.NET pages and server controls.

Namespace: System.Web.UI
Assembly: System.Web (in system.web.dll)

public ref class XhtmlTextWriter : public HtmlTextWriter
public class XhtmlTextWriter extends HtmlTextWriter
public class XhtmlTextWriter extends HtmlTextWriter
Not applicable.

XHTML is an XML-compliant markup language, based on HTML 4.1, which allows you to create Web sites that are suitable for multiple device types. It merges the ease of use provided by HTML with the strict element guidelines provided by XML to produce a markup language with a wide range of format and style options, and with reduced markup tag ambiguity. The XhtmlTextWriter class provides formatting capabilities that ASP.NET server controls use when rendering XHTML content to clients. You can use the SetDocType method to specify which type of XHTML the text writer renders. Supported document types are defined in the XhtmlMobileDocType enumeration.

The XhtmlTextWriter class renders two sets of attributes for elements. One set is a collection of common attributes, as referenced in the CommonAttributes property. The second set is a collection of element-specific attributes, as referenced in the ElementSpecificAttributes property. For more information on the elements and styles that are rendered, see the XHTML modularization specification at the World Wide Web Consortium (W3C) Web site.

You can use the members of the XhtmlTextWriter class and any derived classes to create custom text writers to use in custom XHTML page adapters or class adapters. You can also create derived classes that override the standard behavior of the XhtmlTextWriter class.

By default, when you are working with browsers that support HTML 4.0, ASP.NET pages and controls render markup that is compatible with the XHTML 1.1 standard. For more information, see ASP.NET and XHTML Compliance.

The HtmlTextWriter outputs XHTML unless you configure ASP.NET specifically to not render XHTML markup. For more information, see How to: Configure ASP.NET Web Sites for Non-XHTML Rendering.

The code example in this section contains four parts. The first example demonstrates how to create a derived class. The second code example demonstrates how to create a custom control. The third code example demonstrates how to use the custom control. The fourth code example provides the code that is required to run the custom control.

The following code example demonstrates how to create a custom class that is derived from the XhtmlTextWriter class. It has two constructors, which is standard for all classes that inherit directly or indirectly from the HtmlTextWriter class. The first constructor takes a TextWriter object as a parameter and calls the second constructor, passing the following two parameter values:

  • The TextWriter instance.

  • The value of the HtmlTextWriter.DefaultTabString property, which defines the default line indentation that is used by the XHTML text writer.

This code example also shows how to override the OnAttributeRender and OnStyleAttributeRender methods to filter for text size and color style, respectively. Additionally, it overrides the BeginRender and EndRender methods to write a text string before and after a control has rendered.

No code example is currently available or this language may not be supported.
package Samples.AspNet.JSL; 

import System.*;
import System.IO.*;
import System.Web.*;
import System.Security.Permissions.*;
import System.Web.UI.*;
import System.Web.UI.Adapters.*;
import System.Web.UI.WebControls.Adapters.*;

// Create a class that inherits from XhtmlTextWriter.
/** @attribute AspNetHostingPermission(SecurityAction.Demand, 
    Level = AspNetHostingPermissionLevel.Minimal)
/** @attribute AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)
public class CustomXhtmlTextWriter extends XhtmlTextWriter
    // Create two constructors, following 
    // the pattern for implementing a
    // TextWriter constructor.
    public CustomXhtmlTextWriter(TextWriter writer)
         this(writer, DefaultTabString);
    } //CustomXhtmlTextWriter

    public CustomXhtmlTextWriter(TextWriter writer, String tabString)
         super(writer, tabString);
    } //CustomXhtmlTextWriter

    // Override the OnAttributeRender method to 
    // allow this text writer to render only eight-point 
    // text size.
    protected boolean OnAttributeRender(String name, String value,
        HtmlTextWriterAttribute key)
        if (key.Equals(HtmlTextWriterAttribute.Size)) {
            if (String.Compare(value, "8pt") == 0) {
                return true;
            else {
                return false;
        else {
            return super.OnAttributeRender(name, value, key);
    } //OnAttributeRender

    // Override the OnStyleAttributeRender
    // method to prevent this text writer 
    // from rendering purple text.
    protected boolean OnStyleAttributeRender(String name, String value, 
        HtmlTextWriterStyle key)
        if (key.Equals(HtmlTextWriterStyle.Color)) {
            if (String.Compare(value, "purple") == 0) {
                return false;
            else {
                return true;
        else {
            return super.OnStyleAttributeRender(name, value, key);
    } //OnStyleAttributeRender

    // Override the BeginRender method to write a
    // message and call the WriteBreak method
    // before a control is rendered.
    public void BeginRender()
        this.Write("A control is about to render.");
    } //BeginRender

    // Override the EndRender method to
    // write a string immediately after 
    // a control has rendered. 
    public void EndRender()
        this.Write("A control just rendered.");
    } //EndRender
} //CustomXhtmlTextWriter

The following code example demonstrates how to create a custom Label control named TestLabel and a custom adapter named XhtmlTestLabelAdapter that renders the content of the control as XHTML.

No code example is currently available or this language may not be supported.

The following code example demonstrates how to use the custom control TestLabel on an ASP.NET Web page.

No code example is currently available or this language may not be supported.

To use the custom control in the preceding code example, add the following <controlAdapters> element to one of two files. You can add it to the appropriate machine-wide file in the subdirectory for a specific browser, as a subfolder of the .NET Framework configuration directory. Alternatively, you can add it to a custom browser file in the App_Browsers directory under the Web application root.

   <adapter controlType="AspNet.Samples.TestLabel"
   adapterType="AspNet.Samples.XhtmlTestLabelAdapter" />

  • AspNetHostingPermission  for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
  • AspNetHostingPermission  for operating in a hosted environment. Demand value: InheritanceDemand; Permission value: Minimal.


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0