question

Reborn-0047 avatar image
0 Votes"
Reborn-0047 asked LanHuang-MSFT commented

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

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?

dotnet-csharpdotnet-aspnet-webapi
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

damianoandresini avatar image
0 Votes"
damianoandresini answered damianoandresini commented

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"
});
}

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

not working, are there any other example?

0 Votes 0 ·

If you send me the output wrong result i can help you, before that you can use:
!
while (sdr.Read())
{
draft.Add(new TestClass()
{
UserId = sdr.GetString(0),
Name = sdr.GetString(1),
Mobile = sdr.GetString(2),
Age = !DBNull.Value.Equals(sdr.GetValue(3)) ? sdr.GetValue(3).ToString() : "Null",
Date = !DBNull.Value.Equals(sdr.GetValue(4)) ? sdr.GetValue(4).ToString("YYYY-MM-dd hh:mm:ss") : "Null"
});
}

0 Votes 0 ·

As mentioned from @LanHuang-MSFT have you change your class propwrties (Age and Date) to string? If yes what'is the result of code o the error?

0 Votes 0 ·
LanHuang-MSFT avatar image
0 Votes"
LanHuang-MSFT answered LanHuang-MSFT commented

Hi @Reborn-0047,
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.




image003.png (67.8 KiB)
image002-1.png (5.2 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I don't want to change the database, i just want it the way i want

0 Votes 0 ·

Hi @Reborn-0047,
It does not need to modify the data type in the database,
just modify the data type in the TestClassModel class,
and then use the Convert.ToString Method to convert the data type to a string type.
Best regards,
Lan Huang

0 Votes 0 ·