2013-11-01 60 views
1

類比方說,我有一個看起來像這樣XML序列化與一個不同的名稱

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

我知道如何創建一個名爲ApiException類和序列化到API響應:

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; } 
} 

但如果我alread什麼指定元素名稱你有一個叫做ApiException的班級?說我想打這個FootMouthAPIException

有沒有辦法做到這一點?也許是類本身的數據註釋?

回答

3

您可以使用XmlRoot屬性,例如,

[XmlRoot(ElementName = "ApiException")] 
public class FootMouthAPIException 
{ 
} 
+0

非常好,正是我在找的東西。謝謝! – roryok

相關問題