1

I'm calling a Soap service which is returning Date and Time in two different element. Something like this...

 <ns2:Date>2023-04-27</ns2:Date>
 <ns2:Time>02:34:00+04:00</ns2:Time>

When this mapped into C# object, the value of the time converted to Local container time. Time is of DateTime type.

enter image description here

enter image description here

Here the time is converted to UTC time by considering the timzezone information. But date didn't changed. Ideally, If both date and time coming in one field, then the Date should have been 04/26/2023 22:34:00. Since it is coming in separate field, I'm not able to convert it properly. For time Field, It has a Kind as Local. Can we use this information to convert this properly. Or is there any solution?

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
PaRsH
  • 1,320
  • 4
  • 28
  • 56
  • Does this answer your question? [Implementing Custom XML Serialization/Deserialization of compound data type?](https://stackoverflow.com/questions/22098564/implementing-custom-xml-serialization-deserialization-of-compound-data-type) – Fildor Apr 28 '23 at 08:53
  • Please edit the question to show the corresponding parts of the XML schema if there is one. Also please show or explain how you are parsing the XML (`XDocument` vs `XmlDocument` vs `XmlTextReader`, or something else, etc.). Ideally, provide the code that gave the result in your screenshot, and give that result as text. The screenshot itself is not very useful. Thanks. – Matt Johnson-Pint Apr 28 '23 at 14:35
  • 1
    @MattJohnson-Pint _"The models are generated by the Wsdl."_ (Comment to one of the answers) - Sounds like OP is dealing with WCF. – Fildor Apr 28 '23 at 14:47
  • The models are generated by the wsdl. The worst case scenario, I have to manually change the DataType of Time to DateTimeOffset and not to regenerate the models. But I'm looking for some solution without tweaking it... – PaRsH May 02 '23 at 06:54

2 Answers2

0

You're probably going to want to look at making a custom XML deserializer for this, which combines the two fields then parses them as we date time.

See this example question here: Implementing Custom XML Serialization/Deserialization of compound data type?

0

You can use DateTimeOffset to convert the response into a DateTime using this:

string date = "2023-04-27";
string time = "02:34:00+04:00";
DateTime localDateTime = DateTimeOffset.Parse($"{date}T{time}").ToLocalTime().DateTime;
DateTime utcDateTime = DateTimeOffset.Parse($"{date}T{time}").DateTime;
Console.WriteLine(localDateTime);
Console.WriteLine(utcDateTime);
4/27/2023 1:34:00 AM
4/27/2023 2:34:00 AM

If you share some code how you try to parse, it'll much better.