Error in datetime conversion if XML element date value is null

BeUnique 2,232 Reputation points
2022-02-01T04:53:17.11+00:00

If date field is blank in XML file i am getting error.

see the below screenshot

I am using foreach loop, because i have multiple records in my xml file.

The code is working fine except the date error (if date is blank in xml element ==> Third Record "DateRelieved" is blank.

170091-image.png

below sample xml data
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><RecSearch Val="175"><SearchRes>
<Employee EmpId="ME00001">
<EmpRetired>No</EmpRetired>
<CurrentStatus>true</CurrentStatus>
<DateRelieved>2018-08-20T23:00:00.000Z</DateRelieved>
</Employee>
<Employee EmpId="ME0002">
<EmpRetired></EmpRetired>
<CurrentStatus>true</CurrentStatus>
<DateRelieved>2018-08-20T10:00:00.000Z</DateRelieved>
</Employee>
<Employee EmpId="ME0003">
<EmpRetired></EmpRetired>
<CurrentStatus>true</CurrentStatus>
<DateRelieved></DateRelieved> =======> Date is blank.
</Employee>
</SearchRes>
</RecSearch>

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,108 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 118.5K Reputation points
    2022-02-01T09:32:53.24+00:00

    Try executing this fragment:

    var c = dataSet.Tables["Employee"].Columns["DateRelieved"];
    foreach( DataRow dr in dataSet.Tables["Employee"].Rows )
    {
       if( string.IsNullOrWhiteSpace( dr.Field<string>( c ) ) ) dr[c] = null;
    }
    

    Put it before copy.WriteToServer.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.