1

i am using asp.net web api 2 to do it,i have 2 questions with my code. how can i remove the T in datetime. my date and access have some field is Null and nullable, also how to change it to better format or layout to show null?

the result is in:

<ArrayOfDraft xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Details>
<Access>1</Access>
<Date>2021-11-06T16:04:00</Date>
<Mobile>386754298</Mobile>
<Name>Emily</Name>
<UserId>ABC001</UserId>
</Details>
<Details>
<Access>4</Access>
<Date i:nil"true"/>
<Mobile>386754298</Mobile>
<Name>King</Name>
<UserId>ABD002</UserId>
</Details>

My Code:

public IHttpActionResult Getbyid(int id)
{
    List<TestClass> draft = new List<TestClass>();
    string mainconn = ConfigurationManager.ConnectionStrings["myconn"].ConnectionString;
    SqlConnection sqlconn = new SqlConnection(mainconn);
    string sqlquery = "Select UserID, Name, Mobile, Access, Date From tblTest";
    sqlconn.Open();
    SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
    SqlDataReader sdr = sqlcomm.ExecuteReader();
    while (sdr.Read())
    {
        draft.Add(new TestClass()
            {
                UserId = Convert.ToInt32(sdr.GetValue(0)),
                Name = sdr.GetValue(1).ToString(),
                Mobile = sdr.GetValue(2).ToString(),
                Access = Convert.ToInt32(sdr.GetValue(3)),
                Date = Convert.ToDateTime(sdr.GetValue(4))
            });
    }
    return Ok(draft);
}

How to change it to like this:

<Date>2021-11-06 16:04:00</Date>
and
<Date></Date>
or
<Date>null</Date>
  • Related https://stackoverflow.com/questions/38737699/serialize-datetime-to-xml-in-specific-format-net – Jeremy Lakeman Jul 28 '22 at 03:52
  • Why are you using those `Convert` functions? What is the type of the data in the database? I suspect that the data is all the correct types already so you should not be using `Convert` or `ToString`. You could cast the result of `GetValue` rather than convert but, given that your data reader has `GetString`, `GetInt32` and `GetDateTime` methods, you should be calling those and not even need to cast. – John Jul 28 '22 at 04:27
  • You should also be closing/disposing your data reader and your connection at least. You ought to be creating them and the command with `using` statements and they will then be implicitly disposed at the end of the block. – John Jul 28 '22 at 04:28
  • If the data is nullable then you should also be using the `IsDBNull` method of the data reader to check for null first. Your `TestClass` would then need to have nullable properties if you expect them to be serialised as `null`. – John Jul 28 '22 at 04:31

1 Answers1

1

THere doesn't appear to be a timezone indictator. is this GMT?

This is a close match:

'2018-08-18T07:22:16.0000000Z' --> 8/18/2018 12:22:16 AM

you can use DateTime.Parse (or DateTime.TryParse if you'd rather not throw an exception)

Here is the link to the msdocs: https://learn.microsoft.com/en-us/dotnet/api/system.datetime.parse?view=net-6.0

here is some code:

var dt_str = "2021-11-06T16:04:00";
var dt = System.DateTime.Parse(dt_str);
System.Diagnostics.Trace.TraceInformation($"{dt_str} -> {dt}");
Console.WriteLine($"{dt_str} -> {dt}");

or if you want to use the TryParse:

var dt_str = "2021-11-06T16:04:00";
if (System.DateTime.TryParse(dt_str, out System.DateTime dt)
{
    System.Diagnostics.Trace.TraceInformation($"{dt_str} -> {dt}");
    Console.WriteLine($"{dt_str} -> {dt}");   
}
Glenn Ferrie
  • 10,290
  • 3
  • 42
  • 73