XmlMessageFormatter 類別

定義

使用根據 XSD 結構描述定義的 XML 格式,將物件序列化為訊息主體,或從訊息主體還原序列化為物件。

public ref class XmlMessageFormatter : ICloneable, System::Messaging::IMessageFormatter
public class XmlMessageFormatter : ICloneable, System.Messaging.IMessageFormatter
type XmlMessageFormatter = class
    interface IMessageFormatter
    interface ICloneable
Public Class XmlMessageFormatter
Implements ICloneable, IMessageFormatter
繼承
XmlMessageFormatter
實作

範例

下列程式代碼範例包含三項程式代碼:伺服器元件、訂單類別和用戶端程序代碼。 XSD.exe 公用程式可以使用 order 類別來產生伺服器在傳入訊息內辨識的架構。 架構是 XML 格式的檔案,描述 類別的「圖形」。 然後,此架構可以在用戶端上用來產生與伺服器類別共用相同架構的用戶端特定順序類別。

下列程式代碼範例代表透過消息佇列接收訂單的伺服器元件。 訊息的本文應該是順序物件,其架構符合下列Order.cs類別。 伺服器進程或應用程式會還原串行化順序。

#using <System.dll>
#using <System.Messaging.dll>

using namespace System;
using namespace System::Messaging;

// placeholder; see complete definition elsewhere in this section
public ref class Order
{
public:
   void ShipItems(){}

};


// Creates the queue if it does not already exist.
void EnsureQueueExists( String^ path )
{
   if (  !MessageQueue::Exists( path ) )
   {
      MessageQueue::Create( path );
   }
}

int main()
{
   Console::WriteLine( "Processing Orders" );
   String^ queuePath = ".\\orders";
   EnsureQueueExists( queuePath );
   MessageQueue^ queue = gcnew MessageQueue( queuePath );
   array<String^>^temp0 = {"Order"};
   (dynamic_cast<XmlMessageFormatter^>(queue->Formatter))->TargetTypeNames = temp0;
   while ( true )
   {
      Order^ newOrder = dynamic_cast<Order^>(queue->Receive()->Body);
      newOrder->ShipItems();
   }
}
using System;
using System.Messaging;

 public class Server{

     public static void Main(){

         Console.WriteLine("Processing Orders");

         string queuePath = ".\\orders";
         EnsureQueueExists(queuePath);
         MessageQueue queue = new MessageQueue(queuePath);
         ((XmlMessageFormatter)queue.Formatter).TargetTypeNames = new string[]{"Order"};

         while(true){
             Order newOrder = (Order)queue.Receive().Body;
             newOrder.ShipItems();
         }
     }

     // Creates the queue if it does not already exist.
     public static void EnsureQueueExists(string path){
         if(!MessageQueue.Exists(path)){
             MessageQueue.Create(path);
         }
     }
 }
Imports System.Messaging



Public Class Server
    
    
    Public Shared Sub Main()
        
        Console.WriteLine("Processing Orders")
        
        Dim queuePath As String = ".\orders"
        EnsureQueueExists(queuePath)
        Dim queue As New MessageQueue(queuePath)
        CType(queue.Formatter, XmlMessageFormatter).TargetTypeNames = New String() {"Order"}
        
        While True
            Dim newOrder As Order = CType(queue.Receive().Body, Order)
            newOrder.ShipItems()
        End While
    End Sub
    
    
    ' Creates the queue if it does not already exist.
    Public Shared Sub EnsureQueueExists(path As String)
        If Not MessageQueue.Exists(path) Then
            MessageQueue.Create(path)
        End If
    End Sub
End Class

下列程式代碼範例代表順序類別,提供伺服器上應用程式接收和還原串行化之順序對象的架構。

using namespace System;
public ref class Order
{
public:
   int itemId;
   int quantity;
   String^ address;
   void ShipItems()
   {
      Console::WriteLine( "Order Placed:" );
      Console::WriteLine( "\tItem ID  : {0}", itemId );
      Console::WriteLine( "\tQuantity : {0}", quantity );
      Console::WriteLine( "\tShip To  : {0}", address );
      
      // Add order to the database.
      /* Insert code here. */
   }

};
using System;

 public class Order{

     public int itemId;
     public int quantity;
     public string address;

     public void ShipItems(){

         Console.WriteLine("Order Placed:");
         Console.WriteLine("\tItem ID  : {0}",itemId);
         Console.WriteLine("\tQuantity : {0}",quantity);
         Console.WriteLine("\tShip To  : {0}",address);

         // Add order to the database.
         /* Insert code here. */
     }
 }
Public Class Order
    
    Public itemId As Integer
    Public quantity As Integer
    Public address As String
    
    
    Public Sub ShipItems()
        
        Console.WriteLine("Order Placed:")
        Console.WriteLine(ControlChars.Tab & "Item ID  : {0}", itemId)
        Console.WriteLine(ControlChars.Tab & "Quantity : {0}", quantity)
        Console.WriteLine(ControlChars.Tab & "Ship To  : {0}", address)

        ' Add order to the database.
        ' Insert code here.
 
    End Sub
End Class

與伺服器上應用程式互動的任何用戶端應用程式,都必須將本機定義順序類別中的資訊串行化為訊息本文,將訊息傳送至伺服器。 本機定義的 order 類別必須具有與伺服器定義順序類別相同的架構,伺服器上的應用程式會嘗試還原串行化訊息本文。 XSD.exe 公用程式可讓伺服器上的應用程式管理員建立並散發客戶端必須用來串行化前往伺服器的訊息。

當用戶端應用程式的管理員收到訂單類別的架構時,會再次使用 XSD.exe 公用程式,從架構產生用戶端特定的訂單類別。 此類別用於下列用戶端程式代碼範例中,而不是伺服器的順序類別 (XSD.exe 公用程式會導致架構產生的類別具有與原始類別相同的名稱) 。 這個新的訂單類別可用來將順序串行化為訊息本文。

下列程式代碼範例是客戶端處理,用來串行化訂單,並將與訂單相關聯的信息傳送至佇列。 程式代碼會將 Item、Quantity 和 Address 資訊與由 XSD.exe 公用程式為 Order.cs 類別產生的架構元素產生關聯。 訂單會傳送至本機電腦上的 Orders 佇列。

#using <System.dll>
#using <System.Messaging.dll>

using namespace System;
using namespace System::Messaging;

// placeholder; see complete definition elsewhere in this section
public ref class Order
{
public:
   int itemId;
   int quantity;
   String^ address;
   void ShipItems(){}

};


// Creates the queue if it does not already exist.
void EnsureQueueExists( String^ path )
{
   if (  !MessageQueue::Exists( path ) )
   {
      MessageQueue::Create( path );
   }
}

int main()
{
   String^ queuePath = ".\\orders";
   EnsureQueueExists( queuePath );
   MessageQueue^ queue = gcnew MessageQueue( queuePath );
   Order^ orderRequest = gcnew Order;
   orderRequest->itemId = 1025;
   orderRequest->quantity = 5;
   orderRequest->address = "One Microsoft Way";
   queue->Send( orderRequest );
   
   // This line uses a new method you define on the Order class:
   // orderRequest.PrintReceipt();
}
using System;
using System.Messaging;

 class Client{

     public static void Main(){

         string queuePath = ".\\orders";
         EnsureQueueExists(queuePath);
         MessageQueue queue = new MessageQueue(queuePath);

         Order orderRequest = new Order();
         orderRequest.itemId = 1025;
         orderRequest.quantity = 5;
         orderRequest.address = "One Microsoft Way";

         queue.Send(orderRequest);
         // This line uses a new method you define on the Order class:
         // orderRequest.PrintReceipt();
     }

     // Creates the queue if it does not already exist.
     public static void EnsureQueueExists(string path){
         if(!MessageQueue.Exists(path)){
             MessageQueue.Create(path);
         }
     }
 }
Imports System.Messaging

Class Client
    
    
    Public Shared Sub Main()
        
        Dim queuePath As String = ".\orders"
        EnsureQueueExists(queuePath)
        Dim queue As New MessageQueue(queuePath)
        
        Dim orderRequest As New Order()
        orderRequest.itemId = 1025
        orderRequest.quantity = 5
        orderRequest.address = "One Microsoft Way"
        
        queue.Send(orderRequest)
        ' This line uses a new method you define on the Order class:
        ' orderRequest.PrintReceipt()

    End Sub
    
    ' Creates the queue if it does not already exist.
    Public Shared Sub EnsureQueueExists(path As String)
        If Not MessageQueue.Exists(path) Then
            MessageQueue.Create(path)
        End If
    End Sub
End Class

從伺服器上的 order 類別產生架構之後,您可以修改 類別。 除非架構變更,否則您不需要重新發佈架構。 在您散發架構併產生用戶端順序類別之後,只要未修改架構本身,該客戶端類別也可以獨立於伺服器的順序類別進行修改。 這兩個類別已鬆散結合。

備註

XmlMessageFormatter是的預設格式器,實例MessageQueue會用來串行化寫入佇列的訊息。 當您建立的 MessageQueue實例時,會為您建立 實例 XmlMessageFormatter ,並與相關聯 MessageQueue。 您可以在程式代碼中建立它,並將它指派給 的MessageQueue屬性,Formatter以指定不同的格式器。

佇列的預設 XmlMessageFormatter 實例可用來寫入佇列,但除非您在格式器上設定 TargetTypesTargetTypeNames 屬性,否則無法用來從佇列讀取。 您可以在預設格式子實例上設定其中一個或兩個值,也可以建立格式器的新實例,並藉由將這些值當做自變數傳遞至適當的 XmlMessageFormatter 建構函式來自動設定這些值。

指定 TargetTypes 而非 TargetTypeNames時,會在編譯時期檢查類型是否存在,而不是讀取時間,以減少錯誤的可能性。 TargetTypeNames 要求每個專案都完整,並指定其元件名稱。 此外,使用多個並行版本時,版本號碼也必須附加至目標類型名稱。

TargetTypeNamesTargetTypes 屬性會告訴格式器在還原串行化訊息時,要嘗試比對的架構。 這可讓格式器解譯訊息本文。

訊息本文中串行化的實例必須符合類型數位中所表示的其中一個架構。 當您使用 Receive 方法讀取訊息時,方法會建立型別的物件,該對象對應至所識別的架構,並將訊息本文讀入其中。

從佇列讀取時,只需要設定兩個屬性的其中一個,但您可以設定這兩個屬性。 型別集合是來自兩個屬性的合併集。 要使用哪一個屬性的決策是您的應用程式專屬的。 如果訊息本文包含的類型,其架構不符合任一屬性之陣列中的任何類型,則會在讀取訊息時擲回例外狀況。

XmlMessageFormatter是鬆散結合 XML 型傳訊的重要元件。 XSD.exe 公用程式會使用 XML 格式來產生 XML 架構,例如當您使用 公用程式串行化應用程式所使用的類別時。 類別必須有無參數建構函式。

當公用程式根據您散發的架構來描述類別數據時,會在反向進程中再次使用格式。 使用 公用程式及其產生的 XML 架構可讓您避免每次在類別實作變更之後重新編譯類別時,redistributing.dll 檔案。 只要架構不會在用戶端或伺服器上變更,任一端的其他變更都不會影響另一端。

建構函式

XmlMessageFormatter()

不使用目標型別 (Target Type) 集來初始化 XmlMessageFormatter 類別的新執行個體。

XmlMessageFormatter(String[])

初始化 XmlMessageFormatter 類別的新執行個體,將傳入的目標型別設定為 (完整的) 字串值陣列。

XmlMessageFormatter(Type[])

初始化 XmlMessageFormatter 類別的新執行個體,將傳入的目標型別設定為物件型別的陣列。

屬性

TargetTypeNames

指定可能型別的設定,格式子會從所提供的訊息還原序列化這些型別。

TargetTypes

指定可能型別的設定,格式子會從所提供的訊息還原序列化這些型別。

方法

CanRead(Message)

決定格式子是否能夠還原序列化訊息。

Clone()

建立 XmlMessageFormatter 類別的執行個體,其讀取/寫入屬性 (目標型別集) 與目前的 XmlMessageFormatter 執行個體相同。

Equals(Object)

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

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Read(Message)

讀取指定訊息的內容,並建立包含還原序列化訊息的物件。

ToString()

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

(繼承來源 Object)
Write(Message, Object)

將物件序列化到訊息主體中。

適用於

另請參閱