XML シリアル化を使用した SOAP メッセージの生成
SOAP メッセージは XML を使用して作成されるため、XmlSerializer を使用して、クラスをシリアル化し、エンコード済みの SOAP メッセージを生成できます。結果として生成された XML は、W3C (World Wide Web Consortium) (www.w3.org) のドキュメント『Simple Object Access Protocol (SOAP) 1.1』のセクション 5 に準拠します。SOAP メッセージを使用して通信を行う XML Web サービスを作成する場合は、専用の SOAP 属性のセットをクラスやクラス メンバに適用することで、生成される XML ストリームをカスタマイズできます。属性の一覧については、「エンコード済み SOAP シリアル化を制御する属性」を参照してください。
オブジェクトを SOAP エンコード済み XML ストリームとしてシリアル化するには
XML スキーマ定義ツール (Xsd.exe) を使用してクラスを作成します。
System.Xml.Serialization に含まれている専用属性を 1 つ以上適用します。「エンコード済み SOAP シリアル化を制御する属性」に示す一覧を参照してください。
新しい 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));
XmlTypeMapping を XmlSerializer コンストラクタに渡して、XmlSerializer クラスのインスタンスを作成します。
Dim mySerializer As XmlSerializer = New XmlSerializer(myTypeMapping) [C#] XmlSerializer mySerializer = new XmlSerializer(myTypeMapping);
Serialize メソッドまたは Deserialize メソッドを呼び出します。
エンコード済みの SOAP XML シリアル化のオーバーライド
SOAP メッセージとしてのオブジェクトの XML シリアル化をオーバーライドする処理は、標準の XML シリアル化をオーバーライドする処理と似ています。標準の XML シリアル化をオーバーライドする方法については、「XML シリアル化のオーバーライド」を参照してください。
SOAP メッセージとしてのオブジェクトのシリアル化をオーバーライドするには
- SoapAttributeOverrides クラスのインスタンスを作成します。
- シリアル化するクラス メンバごとに SoapAttributes を作成します。
- シリアル化する対象となるメンバに対し、XML シリアル化に影響する 1 つ以上の属性のインスタンスを適宜作成します。属性の一覧については、「エンコード済み SOAP シリアル化を制御する属性」を参照してください。
- SoapAttributes の適切なプロパティを手順 3 で作成した属性に設定します。
- SoapAttributes を SoapAttributeOverrides に追加します。
- SoapAttributeOverrides を使用して XmlTypeMapping を作成します。SoapReflectionImporter.ImportTypeMapping メソッドを使用します。
- XmlTypeMapping を使用して XmlSerializer を作成します。
- オブジェクトをシリアル化または逆シリアル化します。
ファイルを 2 つの方法でシリアル化する例を次に示します。最初の方法では、XmlSerializer クラスの動作をオーバーライドせずにシリアル化し、2 番目の方法ではオーバーライドしてシリアル化します。この例では、複数のメンバを含む Group
という名前のクラスが使用されています。また、そのクラス メンバに SoapElementAttribute などのさまざまな属性が適用されています。SerializeOriginal
メソッドでこのクラスをシリアル化すると、これらの属性によって SOAP メッセージの内容が制御されます。さまざまな属性を作成し、SoapAttributes のプロパティにそれらの属性を適宜設定することによって、SerializeOverride
メソッドを呼び出したときに、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 シリアル化 | エンコード済み SOAP シリアル化を制御する属性 | XML Web サービスを使用した XML シリアル化