最も簡単な方法は、オブジェクトに対して単純な get を実行して、送り返す XML の例を取得することです。
覚えておくべきこと:
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
null 値を示すために必要な名前空間を含めます
- 標準の XML シリアライザー構成を使用する場合は、名前空間も必要です。
xmlns="http://schemas.datacontract.org/2004/07/MvcApplication10.Controllers"
- get set を実行するとき、accept ヘッダーを
accept: application/xml
- コントローラーに投稿するときは、コンテンツ タイプを設定します。
content-type: application/xml
サンプル オブジェクトとコントローラーの使用。
public class Foo
{
public string Bar { get; set; }
}
public class FoosController : ApiController
{
// GET api/foos
public Foo Get()
{
return new Foo { Bar = "Test" };
}
// GET api/foos
public Foo Post(Foo test)
{
return test;
}
}
/api/foos に対して GET を実行し、サンプル オブジェクトを取得します。
User-Agent: Fiddler
Host: localhost
accept: application/xml
応答:
<Foo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/MvcApplication10.Controllers"><Bar>Testing</Bar></Foo>
ポストバックするには、コンテンツタイプヘッダーを設定して、xml の値を変更し、ポストバックします。
User-Agent: Fiddler
Host: localhost:61280
content-type: application/xml
Content-Length: 167
リクエスト
<Foo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/MvcApplication10.Controllers"><Bar>Testing Response</Bar></Foo
応答
<Foo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/MvcApplication10.Controllers"><Bar>Testing Response</Bar></Foo>
アップデート
複雑な XML モデルの場合は、DataContract 注釈http://msdn.microsoft.com/en-us/library/ms731045.aspxの使用を開始する必要があります。
例:
[DataContract(Name = "Person")]
public class Foo
{
[DataMember(Name = "Address", IsRequired = False)]
public string Bar { get; set; }
}
これにより、必要な XML が変更されます
<Person xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/MvcApplication10.Controllers"><Address>Testing Response</Address></Person>
コレクションなどについては、こちらをご覧ください http://msdn.microsoft.com/en-us/library/aa347850.aspx