[ASP.NET] SOAPのObjectをXMLにシリアライズ

SOAPでは、オブジェクトをXMLにシリアライズして相手に投げることで通信を行っている。
そのXMLが実際にどうなっているかを知る必要が、たまにある。その場合、XmlSerializerを使う。

使い方

例えば、HogeServiceネームスペースのMogeRequestというクラスを引数にとるWebサービスがあり、
mRequestが実際のオブジェクトだとした場合、VB.NETでは以下のような感じ。

Dim formatter As New XmlSerializer(GetType(HogeService.MogeRequest))
Dim strm As System.IO.Stream = New System.IO.FileStream("C:\tmp\sample.xml", System.IO.FileMode.Create)

Try
  formatter.Serialize(strm, mRequest)
Catch ex As Exception
Finally
  strm.Close()
End Try

これで、sample.xmlにHogeService.MogeRequestクラスのmRequestの中がXmlにシリアライズされた内容が書き込まれる。

 

他にもSoapFormatterを使う場面もあるらしい。

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>