私は現在、JSON と共にエンドポイントから XML を出力する必要があるプロジェクトに取り組んでいます。私は次のモデルを持っています:
[DataContract(Namespace="http://www.yale.edu/tp/cas")]
[XmlType("serviceResponse")]
[XmlRoot(Namespace="http://www.yale.edu/tp/cas")]
public class ServiceResponse
{
[XmlElement("authenticationSuccess")]
public AuthenticationSuccess Success { get; set; }
[XmlElement("authenticationFailure")]
public AuthenticationFailure Failure { get; set; }
}
success が null でない場合、出力は次のようになります。
<serviceResponse>
<authenticationSuccess />
</serviceResponse>
ここで、明らかに、要素が含まれるように指定した名前空間にプレフィックスが割り当てられていないことがわかります。私の問題は、メディア フォーマッタを使用して MVC4 に名前空間プレフィックスを追加する場所が見つからないことです。global.asax に次のものがあります。
GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;
GlobalConfiguration.Configuration.Formatters.XmlFormatter.RemoveSerializer(typeof(Models.ServiceResponse));
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SetSerializer(typeof(Models.ServiceResponse), new Infrastructure.NamespaceXmlSerializer(typeof(Models.ServiceResponse)));
XmlSerializer に基づくカスタム シリアライザーを作成して、書き込み要求をインターセプトし、そこに名前空間リストを追加しようとしました。このメソッドの問題は、現在、すべてのオーバーライド可能なメソッド内にブレークポイントがあり、シリアライズ時にそれらのいずれもトリップされないため、シリアライザーが使用されていないと思われることです。
私がやりたいことを達成するための組み込みの方法はありますか、それともオブジェクトをシリアル化するときに名前空間を渡すために XmlMediaTypeFormatter を再実装するのに行き詰まっていますか?