共用方式為


BodyWriter 類別

定義

表示訊息本文的寫入器。

public ref class BodyWriter abstract
public abstract class BodyWriter
type BodyWriter = class
Public MustInherit Class BodyWriter
繼承
BodyWriter
衍生

範例

下列範例將示範如何從 BodyWriter 衍生類別。 這個覆寫會接受字串的陣列,並將其寫入 XmlDictionaryWriter

using System;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Xml;

namespace UEBodyWriter
{
    class MyBodyWriter : BodyWriter
    {
        const string textTag = "text";
        string[] bodySegment;

        public MyBodyWriter(string[] strData) : base(true)
        {
            int length = strData.Length;

            this.bodySegment = new string[length];
            for (int i = 0; i < length; i++)
            {
                this.bodySegment[i] = strData[i];
            }
        }

        protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
        {
           writer.WriteStartElement(textTag);

           foreach (string str in bodySegment)
           {
               writer.WriteString(str);
           }

            writer.WriteEndElement();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            string[] strings = {"Hello", "world"};
            MyBodyWriter bw = new MyBodyWriter(strings);

            StringBuilder strBuilder = new StringBuilder(10);
            XmlWriter writer = XmlWriter.Create(strBuilder);
            XmlDictionaryWriter dictionaryWriter = XmlDictionaryWriter.CreateDictionaryWriter(writer);

            bw.WriteBodyContents(dictionaryWriter);
            dictionaryWriter.Flush();
        }
    }
}


Imports System.Text
Imports System.ServiceModel
Imports System.ServiceModel.Channels
Imports System.Xml

Namespace UEBodyWriter
    Friend Class MyBodyWriter
        Inherits BodyWriter
        Private Const textTag As String = "text"
        Private bodySegment() As String

        Public Sub New(ByVal strData() As String)
            MyBase.New(True)
            Dim length = strData.Length

            Me.bodySegment = New String(length - 1){}
            For i = 0 To length - 1
                Me.bodySegment(i) = strData(i)
            Next i
        End Sub

        Protected Overrides Sub OnWriteBodyContents(ByVal writer As XmlDictionaryWriter)
           writer.WriteStartElement(textTag)

            For Each str As String In bodySegment
                writer.WriteString(str)
            Next str

            writer.WriteEndElement()
        End Sub
    End Class

    Module Module1
        Sub Main(ByVal args() As String)
            Dim strings() As String = {"Hello", "world"}
            Dim bw As New MyBodyWriter(strings)

            Dim strBuilder As New StringBuilder(10)
            Dim writer = XmlWriter.Create(strBuilder)
            Dim dictionaryWriter = XmlDictionaryWriter.CreateDictionaryWriter(writer)

            bw.WriteBodyContents(dictionaryWriter)
            dictionaryWriter.Flush()
        End Sub
    End Module
End Namespace

備註

訊息是由標頭和本文所組成。 標頭會進行緩衝處理,而本文則會進行資料流處理。 由於本文會進行資料流處理,所以使用者無法將本文的實際內容傳遞給訊息。 使用者必須在被要求時,傳遞一個知道如何撰寫本文的類別。 其作法是將衍生自 BodyWriter 的類別傳遞給 Message。 每當訊息要求使用 BodyWriter 寫入本文時,都會呼叫衍生自 XmlWriter 的類別。

建構函式

BodyWriter(Boolean)

初始化 BodyWriter 類別的新執行個體,此類別會明確指出是否要緩衝處理。

屬性

IsBuffered

取得值,這個值指出是否可以多次呼叫寫入方法。

方法

BeginWriteBodyContents(XmlDictionaryWriter, AsyncCallback, Object)

開始以指定的寫入器、回呼及狀態寫入本文寫入器的本文內容。

CreateBufferedCopy(Int32)

建立本文的緩衝處理複本。

EndWriteBodyContents(IAsyncResult)

結束寫入主體內容。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
OnBeginWriteBodyContents(XmlDictionaryWriter, AsyncCallback, Object)

本文寫入器開始以指定寫入器、回呼與狀態寫入本文內容時引發事件。

OnCreateBufferedCopy(Int32)

提供寫入本文內容時的擴充點。

OnEndWriteBodyContents(IAsyncResult)

本文寫入器結束寫入本文內容時引發事件。

OnWriteBodyContents(XmlDictionaryWriter)

當實作時,提供寫入本文內容時的擴充點。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)
WriteBodyContents(XmlDictionaryWriter)

寫出訊息本文的內容。

適用於