Sk93
Sk93

Reputation: 3718

WebService - xsd:Date field

I'm creating a webservice for a customer in ASP.Net v3.5. Currently, we have the an object similar to the following that gets returned by one of the webservice methods:

public class blah
{
 public DateTime datetime;
 public int someData;
}

Now, the customer has sent me the following request:

In your schema you have a single entry for a xsd:dateTime. Can you split that into two fields one for date and the other for time. The use of a xsd:date and xsd:time should be fine as the object types.

Obviously, I can alter the class as follows:

public class blah
{
 public DateTime date;
 public DateTime time;
 public int someData;
}

But I assume that will actually produce two fields of "xsd:DateTime" and not one of each as he is requesting.

Please can you advise how I would achieve the results my customer is expecting?

Upvotes: 0

Views: 743

Answers (2)

Sk93
Sk93

Reputation: 3718

public class Blah
{
 [System.Xml.Serialization.XmlElement(Namespace="http://www.w3.org/2001/XMLSchema", DataType="time")]
 public DateTime time;

 [System.Xml.Serialization.XmlElement(Namespace="http://www.w3.org/2001/XMLSchema", DataType="time")]
 public DateTime date;

 public int someData
}

This seems to be exactly what I'm looking for!

Upvotes: 0

balexandre
balexandre

Reputation: 75073

In .NET there is no Time object, this is part of the DateTime object.

I would go on having two strings objects instead

public class blah
{
    public string date;
    public string time;
    public int someData;
}

and then parse them into a DateTime object soon you get them

DateTime dateOut;
if(DateTime.TryParseExact(
          string.Format("{0} {1}", date, time), 
          "yyyy-MM-dd HH:mm", 
          null, 
          System.Globalization.DateTimeStyles.None, 
          out dateOut)) 
{
   // date is valid
}
else
{
    // send error back saying that DATE or/and Time needs to follow a pattern
}

To make a property assume a specific type you can decorate with:

public class blah
{
    [XmlElement(DataType = "Date")]
    public string date;
    [XmlElement(DataType = "Time")]
    public string time;
    public int someData;
}

but there are forums that .NET has some problems with this... give it a try and test it your own using Fiddler for example to simulate a call to your service.

Upvotes: 1

Related Questions