将XML反序列化为对象



我试图从Amazons的简单数据库服务反序列化xml响应。

xml是这样的。

<ListDomainsResponse xmlns="http://sdb.amazonaws.com/doc/2009-04-15/">
   <ListDomainsResult>
       <DomainName>Logging</DomainName>
       <DomainName>teets</DomainName>
   </ListDomainsResult>
   <ResponseMetadata>
      <RequestId>9d48122e-1ddf-8771-b771-b36599838ea0</RequestId>
     <BoxUsage>0.0000071759</BoxUsage>
   </ResponseMetadata>
</ListDomainsResponse>

我试图序列化到的对象是这样的。

public class ListDomainsResponse  : Response
{
    public ListDomainsResult ListDomainsResult { get; set; }
}
public class ListDomainsResult
{
    public List<String> DomainNames { get; set; }
    public string NextToken { get; set; }
}
public class Response
{
    public static string NameSpace = "http://sdb.amazonaws.com/doc/2009-04-15/";
    public ResponseMetadata ResponseMetadata{ get; set; }
}
public class ResponseMetadata
{
    public string RequestId { get ; set; }
    public string BoxUsage { get; set; }
}

我的问题是,响应元数据被正确地反序列化,但名为DomainName的字符串列表没有被反序列化,每次都是空的。

有人能看出我做错了什么吗。

更新了更多代码

public async Task<Response> ListDomains (ListDomainsRequest request)
{
    using (Client = new HttpClient ()) {
        ListDomainsRequestMarshaller marshaller = new ListDomainsRequestMarshaller ();
        marshaller.Configure (request);
        HttpResponseMessage responseMessage = Client.GetAsync (marshaller.Marshal ()).Result;
        ListDomainsResponseUnMarshaller unmarshaler = new ListDomainsResponseUnMarshaller (responseMessage);
        return unmarshaler.Response;
    }
}
public ListDomainsResponseUnMarshaller (HttpResponseMessage message)
{
    XDocument doc = XDocument.Load (message.Content.ReadAsStreamAsync ().Result);
if (message.StatusCode.Equals (HttpStatusCode.OK)) {
    Serializer = new XmlSerializer (typeof(ListDomainsResponse), Response.NameSpace);
    Response = (ListDomainsResponse)Serializer.Deserialize (doc.CreateReader ());
    Response.HttpStatusCode = message.StatusCode;
    Response.ContentLength = (long)message.Content.Headers.ContentLength;
} else {
    Serializer = new XmlSerializer (typeof(Response));
    Response = (Response)Serializer.Deserialize (doc.CreateReader ());
    Response.HttpStatusCode = message.StatusCode;
    Response.ContentLength = (long)message.Content.Headers.ContentLength;
}
Serializer = null;
}

添加属性有助于

[System.Xml.Serialization.XmlTypeAttribute (Namespace = "http://sdb.amazonaws.com/doc/2009-04-15/")]
[System.Xml.Serialization.XmlRootAttribute ("ListDomainsResponse", Namespace = "http://sdb.amazonaws.com/doc/2009-04-15/")]
public class ListDomainsResponse  : Response
{
    public ListDomainsResult ListDomainsResult { get; set; }
}
[System.Xml.Serialization.XmlTypeAttribute (Namespace = "http://sdb.amazonaws.com/doc/2009-04-15/")]
[System.Xml.Serialization.XmlRootAttribute (Namespace = "http://sdb.amazonaws.com/doc/2009-04-15/")]
public class ListDomainsResult
{
    [System.Xml.Serialization.XmlElementAttribute ("DomainName")]
    public string[] DomainName { get; set; }
    public string NextToken { get; set; }
}

相关内容

  • 没有找到相关文章

最新更新