Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
I have a class and a WebMethod in my C# Web Service Project like this:

public class Results
{
    public int result1;
    public int result2;
    public string str1;
}

[WebMethod]
public Results getResults(int value1, int value2)
{
    Results results = new Results();
    results.result1 = value1 + value2;
    if (value1 > value2)
    {
        results.result2 = value1 - value2;
        results.str1 = "ok";
    }
    return results;
}


When I give values; value1:5 value2:3 then this XML created:

XML
<Results>
<result1>8</result1>
<result2>2</result2>
<str1>ok</str1>
</Results>


And when I give values; value1:5 value2:8 then this XML created:

XML
<Results>
<result1>13</result1>
<result2>0</result2>
</Results>


result2 tag returns 0 value and str1 tag is not visible. C# automaticly creates tag with value of 0 for integers, and no tags for null strings.
How can I get a response like this????:

XML
<Results>
<result1>13</result1>
<result2></result2>
<str1></str1>
</Results>


I want to create all tags with values. If value is null, can i create null tag?
(<str1></str1> or <str1/>)

Thanks.
Posted

1 solution

Use String.Empty instead of null.
 
Share this answer
 
Comments
supreme_tr 23-Aug-11 7:02am    
Thanks. And for integers? They diplays 0 because of initial value. Can i create <result2> null tag for integer?
kornakar 23-Aug-11 8:06am    
You could try nullable types for that.
public int? result1; // notice the question mark
result1.Value = null;
supreme_tr 23-Aug-11 9:33am    
OK :) That works. Thank you.
xsi:nil="true" parameter appears into the tag.
What is this mean? Can I hide this?
kornakar 24-Aug-11 3:02am    
I think you can't. http://stackoverflow.com/questions/2252619/prevent-xsinil-true-on-nullable-value-types-when-serializing-to-xml

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900