1

次のような API 応答があるとします。

<ApiException>
    <Status>400</Status>
    <Message>Foot too big for mouth</Message>
<ApiException>

というクラスを作成し、ApiExceptionそれにシリアル化する方法を知っています。

public class ApiException
{
    public string Status { get; set; }
    public string Message { get; set; }
}

using (var response = ((HttpWebResponse)wex.Response))
{
    using (var reader = new StreamReader(response.GetResponseStream()))
    {
        System.Xml.Serialization.XmlSerializer serializer =
            new System.Xml.Serialization.XmlSerializer(typeof(ApiException));
        ApiException ex = (ApiException)serializer.Deserialize(reader);
    }
}

また、プロパティに要素名を指定する方法も知っています

public class ApiException
{
    [System.Xml.Serialization.XmlElement(ElementName = "Status")]
    public string Whut { get; set; }
    [System.Xml.Serialization.XmlElement(ElementName = "Message")]
    public string Why { get; set; }
}

しかし、すでに というクラスがある場合はどうなるApiExceptionでしょうか? 私はこれを呼び出したいと言うFootMouthAPIException

それを行う方法はありますか?クラス自体のデータ注釈でしょうか?

4

1 に答える 1

3

XmlRoot属性を使用できます。

[XmlRoot(ElementName = "ApiException")]
public class FootMouthAPIException
{
}
于 2013-11-01T12:57:43.793 に答える