how to make null values and empty element in xml have open tags and close tag? Asp.net web api 2

Reborn 1 Reputation point
2022-08-01T09:17:39.09+00:00

i am making a web api and i want to make the open tags and close tags for empty elements and null values. i am using C# and visual basic to code.

My Questions: How to make open tags and close tags for null values and empty elements in xml format with C#?

result outcome:

   <UserId>32</UserId>  
   <Name>Emily</Name>  
   <Mobile>47542942</Mobile>  
   <Age/>  
   <Date i:nil="true"/>  
   <UserId>12</UserId>  
   <Name>Peter</Name>  
   <Mobile>102352134</Mobile>  
   <Age/>  
   <Date i:nil="true"/>  

expect result outcome:

   <UserId>32</UserId>  
   <Name>Emily</Name>  
   <Mobile>47542942</Mobile>  
   <Age></Age>  
   <Date></Date>  
   <UserId>12</UserId>  
   <Name>Peter</Name>  
   <Mobile>102352134</Mobile>  
   <Age></Age>  
   <Date></Date>  

OR

   <UserId>32</UserId>  
   <Name>Emily</Name>  
   <Mobile>47542942</Mobile>  
   <Age>null</Age>  
   <Date>null</Date>  
   <UserId>12</UserId>  
   <Name>Peter</Name>  
   <Mobile>102352134</Mobile>  
   <Age>null</Age>  
   <Date>null</Date>  

My code:

   public IHttpActionResult GetAll()  
           {  
               List<TestClass> draft = new List<TestClass>();  
               string mainconn = ConfigurationManager.ConnectionStrings["myconn"].ConnectionString;  
               SqlConnection sqlconn = new SqlConnection(mainconn);  
               string sqlquery = "SELECT UserID, Name, Mobile, Age, Date FROM tblTest";  
               sqlconn.Open();  
               SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);  
               SqlDataReader sdr = sqlcomm.ExecuteReader();  
               while (sdr.Read())  
               {  
                     draft.Add(new TestClass()  
                     {  
                           UserId = sdr.GetString(0),  
                           Name = sdr.GetString(1),  
                           Mobile = sdr.GetString(2),  
                           Age = (sdr.GetValue(3) != DBNull.Value) ? Convert.ToInt32(sdr.GetValue(3)) : 0,  
                           Date = (sdr.GetValue(4) != DBNull.Value) ? Convert.ToDateTime(sdr.GetValue(4)) : (DateTime)sdr.GetValue(4)  
                     });  
               }  
               return Ok(draft);  
           }  

Is this possible?

Developer technologies ASP.NET ASP.NET API
Developer technologies C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Damiano Andresini 171 Reputation points
    2022-08-01T13:45:36.613+00:00

    You can choice to pass Age and Date as string fields and setting it with constant string value. For example:
    while (sdr.Read())
    {
    draft.Add(new TestClass()
    {
    UserId = sdr.GetString(0),
    Name = sdr.GetString(1),
    Mobile = sdr.GetString(2),
    Age = (sdr.GetValue(3) != DBNull.Value) ? sdr.GetValue(3) : "Null",
    Date = (sdr.GetValue(4) != DBNull.Value) ?sdr.GetValue(4) : "Null"
    });
    }


  2. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2022-08-02T08:15:14.377+00:00

    Hi @Reborn ,
    According to your needs, I think you can change the Age and Date types in your TestClass class to string, and change the Age and Date types of the codebehind to string at the same time, and change the code to the following:

    public IHttpActionResult GetAll()  
            {  
                List<TestClass> draft = new List<TestClass>();  
                string mainconn = ConfigurationManager.ConnectionStrings["myconn"].ConnectionString;  
                SqlConnection sqlconn = new SqlConnection(mainconn);  
                string sqlquery = "SELECT UserId, Name, Mobile, Age, Date FROM tblTest";  
                sqlconn.Open();  
                SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);  
                SqlDataReader sdr = sqlcomm.ExecuteReader();  
                while (sdr.Read())  
                {  
                    draft.Add(new TestClass()  
                    {  
                        UserId = sdr.GetString(0),  
                        Name = sdr.GetString(1),  
                        Mobile = sdr.GetString(2),  
                        Age = (sdr.GetValue(3) != DBNull.Value) ? Convert.ToString(sdr.GetValue(3)) :"null",  
                        Date = (sdr.GetValue(4) != DBNull.Value) ? Convert.ToString(sdr.GetValue(4)) :"null"  
                    });   
                }  
                return Ok(draft);  
            }  
    

    227099-image002-1.png
    227098-image003.png
    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


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.