XhtmlTextWriter クラスとは? わかりやすく解説

XhtmlTextWriter クラス

メモ : このクラスは、.NET Framework version 2.0新しく追加されたものです。

XTHML から派生した XHTML (Extensible Hypertext Markup Language) モジュールすべてのバリエーションを含む XHTML 固有文字を、モバイル デバイス用の ASP.NET サーバー コントロール出力ストリーム書き込みますASP.NET ページサーバー コントロールカスタム XHTML レンダリングを行う場合には XhtmlTextWriter クラスオーバーライドます。

名前空間: System.Web.UI
アセンブリ: System.Web (system.web.dll 内)
構文構文

Public Class XhtmlTextWriter
    Inherits HtmlTextWriter
Dim instance As XhtmlTextWriter
public class XhtmlTextWriter : HtmlTextWriter
public ref class XhtmlTextWriter : public
 HtmlTextWriter
public class XhtmlTextWriter extends HtmlTextWriter
public class XhtmlTextWriter extends
 HtmlTextWriter
解説解説

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.NETXHTML」を参照してください

HtmlTextWriter は、ASP.NETXHTML マークアップレンダリングないよう構成されていない限りXHTML出力します詳細については、「方法 : ASP.NET Web サイトXHTML レンダリング構成する」を参照してください

HtmlTextWriterXhtmlTextWriter との違いは、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);
        }

    }

}

次のコード例では、カスタム コントロール TestLabelASP.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>
.NET Framework のセキュリティ.NET Frameworkセキュリティ
継承階層継承階層
System.Object
   System.MarshalByRefObject
     System.IO.TextWriter
       System.Web.UI.HtmlTextWriter
        System.Web.UI.XhtmlTextWriter
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「XhtmlTextWriter クラス」の関連用語

XhtmlTextWriter クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



XhtmlTextWriter クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS