XhtmlTextWriter クラス
アセンブリ: System.Web (system.web.dll 内)


XHTML は、HTML 4.1 をベースにした XML 準拠のマークアップです。XHTML を使用することで、さまざまなデバイス タイプに適した Web サイトを作成できます。HTML の使いやすさと、XML の厳密な要素規則を組み合わせることによって、幅広い書式とスタイル オプションを備えながら、マークアップ タグのあいまいさを最小限に抑えたマークアップ言語となっています。XhtmlTextWriter クラスには、ASP.NET サーバー コントロールが XHTML コンテンツをクライアントに表示するときに使用する書式指定機能が用意されています。XhtmlTextWriter クラスを使用して、次の XHTML ドキュメント タイプ規格に準拠したマークアップをレンダリングできます。
XhtmlTextWriter クラスは、CommonAttributes プロパティと ElementSpecificAttributes プロパティでそれぞれ参照される共通属性コレクションと要素固有属性コレクションに含まれる XHTML の要素、属性、およびスタイル属性をレンダリングします。レンダリングされる要素とスタイルの詳細については、W3C (World Wide Web Consortium) Web サイトの XHTML モジュール化仕様を参照してください。
XhtmlTextWriter クラスのメンバだけでなく、HtmlTextWriter クラスから継承されたメンバを自由に使用して、カスタム XHTML ページやクラス アダプタで使用するためのカスタム テキスト ライタを作成できます。また、XhtmlTextWriter クラスの標準の動作をオーバーライドする派生クラスを新たに作成することもできます。
既定では、HTML 4.0 以降をサポートするブラウザに対して、ASP.NET ページと ASP.NET コントロールは、XHTML 1.1 規格に準拠したマークアップをレンダリングします。詳細については、「ASP.NET と XHTML」を参照してください。
HtmlTextWriter は、ASP.NET が XHTML マークアップをレンダリングしないように構成されていない限り、XHTML を出力します。詳細については、「方法 : ASP.NET Web サイトで XHTML レンダリングを構成する」を参照してください。
HtmlTextWriter と XhtmlTextWriter との違いは、HtmlTextWriter が通常、デスクトップ ブラウザにマークアップをレンダリングするのに使用されるのに対して、XhtmlTextWriter はモバイル デバイスにマークアップを書き込むのに使用されます。

このセクションには、4 つのコード例が含まれています。最初のコード例では、カスタム派生クラスを作成する方法を示しています。2 つ目のコード例では、カスタム コントロールを作成する方法を示しています。3 つ目のコード例では、カスタム コントロールを使用する方法を示しています。4 つ目のコード例では、カスタム コントロールを実行するために必要なコードを示しています。
次のコード例では、XhtmlTextWriter クラスから派生したカスタム クラスを作成する方法を示しています。2 つのコンストラクタが使用されています。この方法は、HtmlTextWriter クラスを直接または間接的に継承するすべてのクラスに対して標準的な方法です。最初のコンストラクタは、TextWriter オブジェクトをパラメータとして受け取り、2 つ目のコンストラクタを呼び出して、次の 2 つのパラメータ値をこのコンストラクタに渡します。
このコード例では、テキストのサイズとカラー スタイルをそれぞれフィルタ処理するように OnAttributeRender メソッドと OnStyleAttributeRender メソッドをオーバーライドしています。加えて、コントロールをレンダリングする前とした後にテキスト文字列を書き込むように BeginRender メソッドと EndRender メソッドをオーバーライドしています。
Imports System Imports System.IO Imports System.Web Imports System.Security.Permissions Imports System.Web.UI Imports System.Web.UI.Adapters Imports System.Web.UI.WebControls.Adapters Namespace Samples.AspNet.VB ' Create a class that inherits from XhtmlTextWriter. <AspNetHostingPermission(SecurityAction.Demand, _ Level:=AspNetHostingPermissionLevel.Minimal)> _ <AspNetHostingPermission(SecurityAction.InheritanceDemand, _ Level:=AspNetHostingPermissionLevel.Minimal)> _ Public Class CustomXhtmlTextWriter Inherits XhtmlTextWriter ' Create two constructors, following ' the pattern for implementing a ' TextWriter constructor. Public Sub New(writer As TextWriter) MyClass.New(writer, DefaultTabString) End Sub 'New Public Sub New(writer As TextWriter, tabString As String) MyBase.New(writer, tabString) End Sub 'New ' Override the OnAttributeRender method to ' allow this text writer to render only eight-point ' text size. Overrides Protected Function OnAttributeRender(ByVal name As String, _ ByVal value As String, _ ByVal key As HtmlTextWriterAttribute _ ) As Boolean If key = HtmlTextWriterAttribute.Size Then If String.Compare(value, "8pt") = 0 Then Return True Else Return False End If Else Return MyBase.OnAttributeRender(name, value, key) End If End Function ' Override the OnStyleAttributeRender ' method to prevent this text writer ' from rendering purple text. Overrides Protected Function OnStyleAttributeRender(ByVal name As String, _ ByVal value As String, _ ByVal key As HtmlTextWriterStyle _ ) As Boolean If key = HtmlTextWriterStyle.Color Then If String.Compare(value, "purple") = 0 Then Return False Else Return True End If Else Return MyBase.OnStyleAttributeRender(name, value, key) End If End Function ' Override the BeginRender method to write a ' message and call the WriteBreak method ' before a control is rendered. Overrides Public Sub BeginRender() Me.Write("A control is about to render.") Me.WriteBreak() End Sub ' Override the EndRender method to ' write a string immediately after ' a control has rendered. Overrides Public Sub EndRender() Me.Write("A control just rendered.") End Sub End Class End Namespace
using System; using System.IO; using System.Web; using System.Security.Permissions; using System.Web.UI; using System.Web.UI.Adapters; using System.Web.UI.WebControls.Adapters; namespace Samples.AspNet.CS { // Create a class that inherits from XhtmlTextWriter. [AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Minimal)] [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)] public class CustomXhtmlTextWriter : XhtmlTextWriter { // Create two constructors, following // the pattern for implementing a // TextWriter constructor. public CustomXhtmlTextWriter(TextWriter writer) : this(writer, DefaultTabString) { } public CustomXhtmlTextWriter(TextWriter writer, string tabString) : base(writer, tabString) { } // Override the OnAttributeRender method to // allow this text writer to render only eight-point // text size. protected override bool OnAttributeRender(string name, string value, HtmlTextWriterAttribute key) { if (key == HtmlTextWriterAttribute.Size) { if (String.Compare(value, "8pt") == 0) { return true; } else { return false; } } else { return base.OnAttributeRender(name, value, key); } } // Override the OnStyleAttributeRender // method to prevent this text writer // from rendering purple text. protected override bool OnStyleAttributeRender(string name, string value, HtmlTextWriterStyle key) { if (key == HtmlTextWriterStyle.Color) { if (String.Compare(value, "purple") == 0) { return false; } else { return true; } } else { return base.OnStyleAttributeRender(name, value, key); } } // Override the BeginRender method to write a // message and call the WriteBreak method // before a control is rendered. override public void BeginRender() { this.Write("A control is about to render."); this.WriteBreak(); } // Override the EndRender method to // write a string immediately after // a control has rendered. override public void EndRender() { this.Write("A control just rendered."); } } }
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."); this.WriteBreak(); } //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
次のコード例では、TestLabel という名前のカスタム Label コントロールと、コントロールの内容を XHTML にレンダリングする XhtmlTestLabelAdapter という名前のカスタム アダプタを作成する方法を示しています。
Imports System Imports System.Web Imports System.Web.UI Imports System.Web.UI.Adapters Imports System.Web.UI.WebControls Imports System.Web.UI.WebControls.Adapters Namespace AspNet.Samples ' Create a simple class that inherits ' from the Label class. Public Class TestLabel Inherits Label Private textValue As String ' Override the Text property. Overrides Public Property Text As String Get Return CStr(ViewState("Text")) End Get Set ViewState("Text") = Value End Set End Property End Class ' Create a class to render the custom Label's ' content to XHTML devices. Public Class XhtmlTestLabelAdapter Inherits WebControlAdapter ' Create a Control property that accesses the ' methods and properties of the control. Protected Shadows ReadOnly Property Control() As TestLabel Get Return CType(MyBase.Control, TestLabel) End Get End Property ' Override the Render method. Protected Overrides Sub Render(ByVal writer As HtmlTextWriter) ' Create an instance of the XhtmlTextWriter class, ' named w, and cast the HtmlTextWriter passed ' in the writer parameter to w. Dim w As XhtmlTextWriter = New XhtmlTextWriter(writer) ' Create a string variable, named value, to hold ' the control's Text property value. Dim value As String = Control.Text ' Create a Boolean variable, named attTest, ' to test whether the Style attribute is ' valid in the page that the control is ' rendered to. Dim attTest As Boolean = w.IsValidFormAttribute("style") ' Check whether attTest is true or false. ' If true, a style is applied to the XHTML ' content. If false, no style is applied. If (attTest = True) Then w.EnterStyle(Control.ControlStyle) End If ' Write the Text property value of the control, ' a <br> element, and a string. w.Write(value) w.WriteBreak() w.Write("This control conditionally rendered its styles for XHTML.") ' Check whether attTest is true or false. ' If true, the XHTML style is closed. ' If false, nothing is rendered. If (attTest = True) Then w.ExitStyle(Control.ControlStyle) End If End Sub 'Render End Class End Namespace
using System; using System.Web; using System.Web.UI; using System.Web.UI.Adapters; using System.Web.UI.WebControls; using System.Web.UI.WebControls.Adapters; namespace AspNet.Samples { // Create a simple class that inherits // from the Label class. public class TestLabel : Label { private String _textValue; // Override the Text property. public override string Text { get { return (string)ViewState["Text"]; } set { ViewState["Text"] = value; } } } public class XhtmlTestLabelAdapter : WebControlAdapter { // Create a control property that accesses the // methods and properties of the control. protected TestLabel Control { get { return (TestLabel)base.Control; } } protected override void Render(HtmlTextWriter writer) { // Create an instance of the XhtmlTextWriter class, // named w, and cast the HtmlTextWriter passed // in the writer parameter to w. XhtmlTextWriter w = new XhtmlTextWriter(writer); // Create a string variable, named value, to hold // the control's Text property value. String value = Control.Text; // Create a Boolean variable, named attTest, // to test whether the Style attribute is // valid in the page that the control is // rendered to. Boolean attTest = w.IsValidFormAttribute("style"); // Check whether attTest is true or false. // If true, a style is applied to the XHTML // content. If false, no style is applied. if (attTest) w.EnterStyle(Control.ControlStyle); // Write the Text property value of the control, // a <br> element, and a string. w.Write(value); w.WriteBreak(); w.Write("This control conditionally rendered its styles for XHTML."); // Check whether attTest is true or false. // If true, the XHTML style is closed. // If false, nothing is rendered. if (attTest) w.ExitStyle(Control.ControlStyle); } } }
次のコード例では、カスタム コントロール TestLabel を ASP.NET Web ページで使用する方法を示しています。
<%@ Page Language="VB" %> <%@ Import Namespace="AspNet.Samples" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim tl As TestLabel = New TestLabel() tl.ID = "TestLabel1" PlaceHolder1.Controls.Add(tl) End Sub </script> <html > <head runat="server"> <title>XHtmlTextWriter Example</title> </head> <body> <form id="form1" runat="server" > <div> <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder> </div> </form> </body> </html>
<%@ Page Language="C#" %> <%@ Import Namespace="AspNet.Samples" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { TestLabel tl = new TestLabel(); tl.ID = "TestLabel1"; PlaceHolder1.Controls.Add(tl); } </script> <html > <head runat="server" title="test"> <title>XHtmlTextWriter Example</title> </head> <body> <form id="form1" runat="server" > <div> <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder> </div> </form> </body> </html>
前述のコード例のカスタム コントロールを使用するには、.NET Framework 構成ディレクトリのブラウザ用サブディレクトリ内の該当するマシン レベルのファイル、または Web アプリケーション ルート下の App_Browsers ディレクトリ内のカスタム ブラウザ ファイルのいずれかに次のコードを追加します。
<controlAdapters> <adapter controlType="AspNet.Samples.TestLabel" adapterType="AspNet.Samples.XhtmlTestLabelAdapter" /> </controlAdapters>


System.MarshalByRefObject
System.IO.TextWriter
System.Web.UI.HtmlTextWriter
System.Web.UI.XhtmlTextWriter


Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- XhtmlTextWriter クラスのページへのリンク