共用方式為


使用 XML 序列化產生 SOAP 訊息

由於 SOAP 訊息是使用 XML 建置 (Build) 的,因此 XmlSerializer 可用來序列化類別和產生編碼 SOAP 訊息。產生的 XML 與全球資訊網協會 (www.w3.org) 文件<Simple Object Access Protocol (SOAP) 1.1>第五節相符。當您建立透過 SOAP 訊息通訊的 XML Web Service 時,您可將一組特殊的 SOAP 屬性套用至類別和類別的成員來自訂 XML 資料流。如需屬性的清單,請參閱控制編碼 SOAP 序列化的屬性

若要將物件序列化為 SOAP 編碼的 XML 資料流

  1. 使用 XML 結構描述定義工具 (Xsd.exe) 建立類別。

  2. 套用一或多個在 System.Xml.Serialization 中找到的特殊屬性。請參閱<控制編碼 SOAP 序列化的屬性>中的清單。

  3. 建立新的 SoapReflectionImporter,接著利用序列化類別的型別叫用 ImportTypeMapping 方法來建立 XmlTypeMapping

    下列範例會呼叫 SoapReflectionImporter 類別的 ImportTypeMapping 方法來建立 XmlTypeMapping

    ' Serializes a class named Group as a SOAP message.
    Dim myTypeMapping As XmlTypeMapping = (New SoapReflectionImporter(). _
    ImportTypeMapping(GetType(Group))
    [C#]
    // Serializes a class named Group as a SOAP message.
    XmlTypeMapping myTypeMapping = (new SoapReflectionImporter().
    ImportTypeMapping(typeof(Group));
    
  4. XmlTypeMapping 傳遞至 XmlSerializer 建構函式來建立 XmlSerializer 的執行個體。

    Dim mySerializer As XmlSerializer = New XmlSerializer(myTypeMapping)
    [C#]
    XmlSerializer mySerializer = new XmlSerializer(myTypeMapping);
    
  5. 呼叫 SerializeDeserialize 方法。

覆寫編碼 SOAP XML 序列化

將物件的 XML 序列化覆寫為 SOAP 訊息的處理序與覆寫標準 XML 序列化的處理序類似 (如需覆寫標準 XML 序列化的詳細資訊,請參閱覆寫 XML 序列化)。

若要覆寫 SOAP 訊息等物件的序列化

  1. 建立 SoapAttributeOverrides 類別的執行個體。
  2. 為要進行序列化的每個類別成員建立 SoapAttributes
  3. 視需要將會影響 XML 序列化的一或多個屬性 (如需清單,請參閱<控制編碼 SOAP 序列化的屬性>) 的執行個體建立至要進行序列化的成員。
  4. SoapAttributes 的適當屬性 (Property) 設定為在步驟 3 中建立的屬性 (Attribute)。
  5. SoapAttributes 加入 SoapAttributeOverrides
  6. 使用 SoapAttributeOverrides 建立 XmlTypeMapping。使用 SoapReflectionImporter.ImportTypeMapping 方法。
  7. 使用 XmlTypeMapping 建立 XmlSerializer
  8. 序列化或還原序列化物件。

下面的範例會以兩種方式序列化檔案:第一種方式是不覆寫 XmlSerializer 類別的行為,第二種則是覆寫行為。範例中包含具有幾個成員且名為 Group 的類別。範例已將幾個屬性 (例如 SoapElementAttribute) 套用至類別成員。當使用 SerializeOriginal 方法序列化類別時,屬性會控制 SOAP 訊息內容。當呼叫 SerializeOverride 方法時,就會建立幾個屬性 (Attribute) 並視需要將 SoapAttributes 的屬性 (Property) 設定為這些屬性 (Attribute),藉此覆寫 XmlSerializer 的行為。

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;

public class Group
{
    [SoapAttribute (Namespace = "http://www.cpandl.com")]
    public string GroupName;
    
    [SoapAttribute(DataType = "base64Binary")]
    public Byte [] GroupNumber;

    [SoapAttribute(DataType = "date", AttributeName = "CreationDate")]
    public DateTime Today;
    [SoapElement(DataType = "nonNegativeInteger", ElementName = "PosInt")]
    public string PostitiveInt;
    // This is ignored when serialized unless it is overridden.
    [SoapIgnore] 
    public bool IgnoreThis;

    public GroupType Grouptype;
    
    [SoapInclude(typeof(Car))]
    public Vehicle myCar(string licNumber)
    {
        Vehicle v;
        if(licNumber == "")
            {
                v = new Car();
            v.licenseNumber = "!!!!!!";
        }
        else
        {
            v = new Car();
            v.licenseNumber = licNumber;
        }
        return v;
    }
}

public abstract class Vehicle
{
    public string licenseNumber;
    public DateTime makeDate;
}

public class Car: Vehicle
{
}

public enum GroupType
{
    // These enums can be overridden.
    small,
    large
}
    
public class Run
{
    public static void Main()
    {
        Run test = new Run();
        test.SerializeOriginal("SoapOriginal.xml");
        test.SerializeOverride("SoapOverrides.xml");
        test.DeserializeOriginal("SoapOriginal.xml");
        test.DeserializeOverride("SoapOverrides.xml");
    
    }
    public void SerializeOriginal(string filename)
    {
        // Creates an instance of the XmlSerializer class.
        XmlTypeMapping myMapping = 
        (new SoapReflectionImporter().ImportTypeMapping(
        typeof(Group)));
        XmlSerializer mySerializer =  
        new XmlSerializer(myMapping);

        // Writing the file requires a TextWriter.
        TextWriter writer = new StreamWriter(filename);

        // Creates an instance of the class that will be serialized.
        Group myGroup = new Group();

        // Sets the object properties.
        myGroup.GroupName = ".NET";

        Byte [] hexByte = new Byte[2]{Convert.ToByte(100),
        Convert.ToByte(50)};
        myGroup.GroupNumber = hexByte;

        DateTime myDate = new DateTime(2002,5,2);
        myGroup.Today = myDate;

        myGroup.PostitiveInt= "10000";
        myGroup.IgnoreThis=true;
        myGroup.Grouptype= GroupType.small;
        Car thisCar =(Car)  myGroup.myCar("1234566");

        // Prints the license number just to prove the car was created.
        Console.WriteLine("License#: " + thisCar.licenseNumber + "\n");

        // Serializes the class, and closes the TextWriter.
        mySerializer.Serialize(writer, myGroup);
        writer.Close();
    }

    public void SerializeOverride(string filename)
    {
        // Creates an instance of the XmlSerializer class
        // that overrides the serialization.
        XmlSerializer overRideSerializer = CreateOverrideSerializer();

        // Writing the file requires a TextWriter.
        TextWriter writer = new StreamWriter(filename);

        // Creates an instance of the class that will be serialized.
        Group myGroup = new Group();

        // Sets the object properties.
        myGroup.GroupName = ".NET";

        Byte [] hexByte = new Byte[2]{Convert.ToByte(100),
        Convert.ToByte(50)};
        myGroup.GroupNumber = hexByte;

        DateTime myDate = new DateTime(2002,5,2);
        myGroup.Today = myDate;


        myGroup.PostitiveInt= "10000";
        myGroup.IgnoreThis=true;
        myGroup.Grouptype= GroupType.small;
        Car thisCar =(Car)  myGroup.myCar("1234566");

        // Serializes the class, and closes the TextWriter.
        overRideSerializer.Serialize(writer, myGroup);
         writer.Close();
    }

    public void DeserializeOriginal(string filename)
    {
        // Creates an instance of the XmlSerializer class.
        XmlTypeMapping myMapping = 
        (new SoapReflectionImporter().ImportTypeMapping(
        typeof(Group)));
        XmlSerializer mySerializer =  
        new XmlSerializer(myMapping);

        TextReader reader = new StreamReader(filename);

        // Deserializes and casts the object.
        Group myGroup; 
        myGroup = (Group) mySerializer.Deserialize(reader);

        Console.WriteLine(myGroup.GroupName);
        Console.WriteLine(myGroup.GroupNumber[0]);
        Console.WriteLine(myGroup.GroupNumber[1]);
        Console.WriteLine(myGroup.Today);
        Console.WriteLine(myGroup.PostitiveInt);
        Console.WriteLine(myGroup.IgnoreThis);
        Console.WriteLine();
    }

    public void DeserializeOverride(string filename)
    {
        // Creates an instance of the XmlSerializer class.
        XmlSerializer overRideSerializer = CreateOverrideSerializer();
        // Reading the file requires a TextReader.
        TextReader reader = new StreamReader(filename);

        // Deserializes and casts the object.
        Group myGroup; 
        myGroup = (Group) overRideSerializer.Deserialize(reader);

        Console.WriteLine(myGroup.GroupName);
        Console.WriteLine(myGroup.GroupNumber[0]);
        Console.WriteLine(myGroup.GroupNumber[1]);
        Console.WriteLine(myGroup.Today);
        Console.WriteLine(myGroup.PostitiveInt);
        Console.WriteLine(myGroup.IgnoreThis);
    }

    private XmlSerializer CreateOverrideSerializer()
    {
        SoapAttributeOverrides mySoapAttributeOverrides = 
        new SoapAttributeOverrides();
        SoapAttributes soapAtts = new SoapAttributes();

        SoapElementAttribute mySoapElement = new SoapElementAttribute();
        mySoapElement.ElementName = "xxxx";
        soapAtts.SoapElement = mySoapElement;
        mySoapAttributeOverrides.Add(typeof(Group), "PostitiveInt", 
        soapAtts);

        // Overrides the IgnoreThis property.
        SoapIgnoreAttribute myIgnore = new SoapIgnoreAttribute();
        soapAtts = new SoapAttributes();
        soapAtts.SoapIgnore = false;      
        mySoapAttributeOverrides.Add(typeof(Group), "IgnoreThis", 
        soapAtts);

        // Overrides the GroupType enumeration.
        soapAtts = new SoapAttributes();
        SoapEnumAttribute xSoapEnum = new SoapEnumAttribute();
        xSoapEnum.Name = "Over1000";
        soapAtts.SoapEnum = xSoapEnum;

        // Adds the SoapAttributes to the 
        // mySoapAttributeOverridesrides.
        mySoapAttributeOverrides.Add(typeof(GroupType), "large", 
        soapAtts);

        // Creates a second enumeration and adds it.
        soapAtts = new SoapAttributes();
        xSoapEnum = new SoapEnumAttribute();
        xSoapEnum.Name = "ZeroTo1000";
        soapAtts.SoapEnum = xSoapEnum;
        mySoapAttributeOverrides.Add(typeof(GroupType), "small", 
        soapAtts);

        // Overrides the Group type.
        soapAtts = new SoapAttributes();
        SoapTypeAttribute soapType = new SoapTypeAttribute();
        soapType.TypeName = "Team";
        soapAtts.SoapType = soapType;
        mySoapAttributeOverrides.Add(typeof(Group),soapAtts);

        // Creates an XmlTypeMapping that is used to create an instance 
        // of the XmlSerializer class. Then returns the XmlSerializer.
        XmlTypeMapping myMapping = (new SoapReflectionImporter(
        mySoapAttributeOverrides)).ImportTypeMapping(typeof(Group));
    
        XmlSerializer ser = new XmlSerializer(myMapping);
        return ser;
    }
}

請參閱

XML 序列化 | 控制編碼 SOAP 序列化的屬性 | XML Web Service 的 XML 序列化