Reputation: 282
I am integrating with a web service (i dont have control to this web service) using wsdl provided. While calling a method, i need to pass DateTime in request. The request needs to contain datetime in UTC format (with Z in the end). The request contains below field,
[System.Xml.Serialization.XmlElementAttribute(DataType="date", Order=0)]
public System.DateTime date
{
get
{
return this.dateField;
}
set
{
this.dateField = value;
}
Please note the xsd datatype is a date.
I construct the request to pass DateTime
as Utc,
request.date = DateTime.SpecifyKind(DateTime.Parse(DateTime.Now.ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'")), DateTimeKind.Utc);
The problem i have is even though i pass DateTime
as Utc, the soap request appears without the time zone. For eg, the request gets generated as shown below,
<GetRequest xmlns=" http://soa.company.com/services/example/v2">
<date>2001-01-01</date>
</GetRequest>
My expectation is to get,
<GetRequest xmlns=" http://soa.company.com/services/example/v2">
<date>2001-01-01Z</date>
</GetRequest>
I think this is due to roundtrip during datetime serialization. Has anybody faced this kind of issue?
Upvotes: 3
Views: 6991
Reputation: 11
From another stackoverflow link dealing with a separate issue, you may want to consider the following code for setting up your datetime struct:
C# DateTime to UTC Time without changing the time
other gets treated as UTC time based on the time specified by datetime and serializes with the "Z" at the end in the web service. I was having the same issue, but rather than try to fix the serialization code, the root issue for me seemed to be the datetime member wasn't being treated as UTC time.
Upvotes: 1
Reputation: 282
Ok , Finally solved this.Link Force XmlSerializer to serialize DateTime as 'YYYY-MM-DD hh:mm:ss' was very useful. I added a string attribute similar to the one described in the link above and that seems to have fixed the issue.
/// <remarks/>
[System.Xml.Serialization.XmlIgnore]
public System.DateTime date
{
get
{
return this.dateField;
}
set
{
this.dateField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("date", Order = 0)]
public System.String somedate
{
get { return this.date.ToString("yyyy'-'MM'-'dd'Z'"); }
set { this.date = System.DateTime.Parse(value); }
}
However, modifying generated proxy is definitely not the preferred way.
Upvotes: 1
Reputation: 5715
The easiest thing you can do is making an agreement that everybody must be always pass DateTime
as UTC. This way, you reduce the process time (to serialize timezone) and data size.
Upvotes: 2