I want to convert a JSON response to an equivalent XML document but while doing so, I also want to preserve current date and time format along with the offset e.g., DateTime in JSON is "2019-10-25T07:00:00-05:00" and after conversion I want it to remain the same. But after conversion into XML, the DateTime value becomes "2019-10-25T08:00:00-04:00"
I tried to search about it in Microsoft docs but I didn't find my answer for the following questions:
- How to determine the time zone of a given date time string (e.g., "2019-10-25T07:00:00-05:00")?
- How can I convert a date time string (e.g., "2019-10-25T08:00:00-04:00") into a date time of the desired time-zone (e.g., into the time zone of "2019-10-25T07:00:00-05:00")
// C# Code Snippet
// Step 1: Reading JsonResponse from a file
string jsonString = System.IO.File.ReadAllText(@"C:\TestDateTimeConvertJSONResponse.txt");
// Step 2: Converting jsonString to XMLDoc
System.Xml.XmlDocument xmlDoc = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(jsonString);
Console.WriteLine();
Console.WriteLine("EQUIVALENT XML RESPONSE");
Console.WriteLine(xmlDoc.InnerXml);
Input JSON string:
{
"Flight": {
"FlightNumber": "747",
"Source": "JFK",
"Destination": "LAS",
"Status": "ON TIME",
"DepDateTime": "2019-10-25T07:00:00-05:00",
"Terminal": "2"
}
}
Expected:
<Flight>
<FlightNumber>747</FlightNumber>
<Source>JFK</Source>
<Destination>LAS</Destination>
<Status>ON TIME</Status>
<DepDateTime>2019-10-25T07:00:00-05:00</DepDateTime>
<Terminal>2</Terminal>
</Flight>
Actual:
<Flight>
<FlightNumber>747</FlightNumber>
<Source>JFK</Source>
<Destination>LAS</Destination>
<Status>ON TIME</Status>
<DepDateTime>2019-10-25T08:00:00-04:00</DepDateTime>
<Terminal>2</Terminal>
</Flight>