简体   繁体   中英

How to make a right DTO class for request XML body on consuming Web API service

I am using RestSharp with C# codes to PUT data object (my_object) for a WEB API service operation that supports only XML format.

My question: How can I make a right DTO class (eg Analytics, please see below) such that on calling request.AddBody(my_object) method of RestSharp, RestSharp is able to convert a data piece Timestamp to XML attribute along with other properties of Analytics DTO class?

my_object in method call request.AddBody(my_object) is custom AnalyticsLogs DTO class (please see below).

The following is an example request Xml body the Web API service operation requires:

<AnalyticsLogs>
  <Analytics Timestamp="1999-05-31T11:20:00">
    <UserExpId>9223372036854775807</UserExpId>
    <UserExpStatus>String content</UserExpStatus>
    <Category>String content</Category>
    <Value>2147483647</Value>
  </Analytics>
  <Analytics Timestamp="2007-05-12T11:20:00">
    <UserExpId>8223372036854775899</UserExpId>
    <UserExpStatus>String content</UserExpStatus>
    <Category>String content</Category>
    <Value>2147483647</Value>
  </Analytics>
</AnalyticsLogs>

I am planning to make some custom DTO classes like below. But for custom Analytics DTO class, I do not know how to make Timestamp data as an XML attribute according to the above request Xml body. Please advice.

public class AnalyticsLogs : List ;

public class Analytics
{
   public long UserExpId { get;set;}
   public string UserExpStatus { get; set;}
   public string Category { get;set;}
   public int Value {get; set;}

   // how can I do for XML attribute Timestamp ???  It seems it can not a class property but I am not sure.
   ???
}

Thank you.

XmlAttribte @msdn

public class Analytics
{
   public long UserExpId { get;set;}
   public string UserExpStatus { get; set;}
   public string Category { get;set;}
   public int Value {get; set;}

   [XmlAttribute]
   public DateTime Timestamp { get; set; }
}

For RestSharp you use attribute SerializeAsAttribute . This is defined here .

public class Analytics
{
   public long UserExpId { get;set;}
   public string UserExpStatus { get; set;}
   public string Category { get;set;}
   public int Value {get; set;}

   [SerializeAsAttribute]
   public DateTime Timestamp { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM