473,385 Members | 1,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

optional nillable values

Hi,

I have added an optional nillable element to a schema like this -
....
<xs:element minOccurs="0" maxOccurs="1" name="Id" type="xs:int"
nillable="true"/>
....
A class is generated from this using xsd.exe and which is used by a web
service where it makes it's way into the wsdl as:

<s:element minOccurs="0" maxOccurs="1" name="Id" nillable="true"
type="s:int" />
Now when the client consumes the web service I get a class

class Thing
{
[System.Xml.Serialization.XmlElementAttribute(IsNul lable=true)]
public System.Nullable<int> Id;

[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool IdSpecified {

}

Which is what I expected. Now however if I specify

Thing t = new Thing();
t.Id = 1;
t.IdSpecified = true;

and send this to the web service it is received as Id=1, IdSpecified=false
which is not what I expected.

Similarly when data is retrieved from the web service the IdSpecified is
always false even if there's data in Id.

I would really expect to be able to optionally send a value or a null value.

Regards
Phil Lee


Jan 18 '06 #1
6 2379


Phil Lee wrote:

I have added an optional nillable element to a schema like this -
...
<xs:element minOccurs="0" maxOccurs="1" name="Id" type="xs:int"
nillable="true"/>
...
A class is generated from this using xsd.exe and which is used by a web
service where it makes it's way into the wsdl as:

<s:element minOccurs="0" maxOccurs="1" name="Id" nillable="true"
type="s:int" />


You might want to ask in
microsoft.public.dotnet.framework.aspnet.webservic es for web service
related stuff.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jan 18 '06 #2
Hi Phil,

I think you can modify the IdSpecified method like the following:

[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool IdSpecified {
if(this.Id.HasValue)
return true;
else
return false;
}

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jan 19 '06 #3
Kevin,

ok, thanks. I don't like modifying auto-generated code though - it tends to
get overwritten.

Is this a bug? Or do I not understand nillable correctly - which is quite
possible.

I understood or hoped that nillable meant that an explicit 'nil' value could
be sent to represent a null. Which is different from not sending the
element. So I would expect to be able to:
1) not send the element which would result in the Specified flag being
false
2) send the element with a value which would result in the flag being
true and the value being non-null
3) send the element with a nil-value which would result in the flag
being true and the value being null
which would then be represented quite nicely by a System.Nullable<> and a
FieldSpecified flag.

Regards
Phil Lee

"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:mO**************@TK2MSFTNGXA02.phx.gbl...
Hi Phil,

I think you can modify the IdSpecified method like the following:

[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool IdSpecified {
if(this.Id.HasValue)
return true;
else
return false;
}

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jan 19 '06 #4
Hi Phil,

Thanks for your feedback. Yes, it seems to be a bug, with my search. You
can check the following link for detail.

http://lab.msdn.microsoft.com/Produc...x?feedbackid=c
1dbdc79-db80-4cc1-b392-e6f6e457db85

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jan 20 '06 #5
Kevin,

I think what I'm asking is slightly different from the "bug" you refer to.

If I have this class/schema:

....
<xs:element minOccurs="0" maxOccurs="1" name="Id" type="xs:int"/>
....

class Customer
{
public int Id;
[XmlIgnore()]
public bool IdSpecified;
}

I can use it like this:

Customer test = new Customer();
test.Id = 2;
test.IdSpecified = true;

now send it somewhere via xml serialization and it arrives with Id=2 and
IdSpecified=true. Note there isn't any special code to set the specified
flag to true when
the Id field is set. It just happens as part of the deserialization
process.

If I have this (nillable value):

....
<xs:element minOccurs="0" maxOccurs="1" name="Id" type="xs:int"
nillable="true"/>
....

class Customer
{
public int? Id;
[XmlIgnore()]
public bool IdSpecified;
}

And I use it in the same way:

Customer test = new Customer();
test.Id = 2;
test.IdSpecified = true;

If I send it somewhere via xml serialization it arrives with Id=2 and
IdSpecified=false which is wrong. The specified flag is always false after
deserialization
regardless of the value of Id and of the specified flag before
serialization.

Look at these examples:

Value being sent before serialization: IdSpecified=true, Id=null
Serialized xml: <Customer> <Id
xsi:null="true" /> </Customer>
Deserialized: IdSpecified=false, Id=null

Value being sent before serialization: IdSpecified=false, Id=null or any
non-null value
Serialized xml: <Customer> </Customer>
Deserialized: IdSpecified=false, Id=null

Value being sent before serialization: IdSpecified=true, Id=4
Serialized xml: <Customer> <Id>4</</Id>
</Customer>
Deserialized: IdSpecified=false, Id=4

The serialized xml is what I would expect. The deserialized data is wrong
because there is no way to distinguish a missing (optional) value from an
explict null value.

Regards
Phil

"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:Cj**************@TK2MSFTNGXA02.phx.gbl...
Hi Phil,

Thanks for your feedback. Yes, it seems to be a bug, with my search. You
can check the following link for detail.

http://lab.msdn.microsoft.com/Produc...x?feedbackid=c
1dbdc79-db80-4cc1-b392-e6f6e457db85

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."


Jan 20 '06 #6
Hi Phil,

It seems to have something to do with the web service. I suggest you try to
post it in the web service newsgroup. Maybe there will be more
professionals who can answer this question there.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jan 23 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Erik Klein | last post by:
I have a WebService whose WSDL contains the following snippet: <complexType abstract="true" name="Type"> <sequence> <element name="localId" nillable="true" type="xsd:string"/> <element...
13
by: William Ryan | last post by:
I just picked up a copy of John Robbins' debugging book and started to look at disassembled code. Anyway, I hate optional Parameters in VB, but I was checking them out to see what IL is created. ...
8
by: Marc | last post by:
Hi! I'm calling a web service using C# and a wrapper class generated by wsdl tool. The web service specification contains some nillable parameters which types are Value Types in .NET (long, int,...
2
by: svestin | last post by:
Hi All! I run into a problem defining a XSD schema with KEYREF references. Is it possible to use KEYREF with nillable fields? Just like a database where a FK could be null. In the example...
14
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
0
by: Mahesh BS | last post by:
Hello, I have developed a webservice using C# and exposed a web method as follow:
1
by: Daniel Serodio | last post by:
Is it possible to have an element which is both restricted by a pattern and nillable? Example schema: <xs:simpleType name="cpfType"> <xs:restriction base="xs:string"> <xs:pattern...
1
by: Glenn | last post by:
I am writing a Web Service in C# ASP.NET 2.0 that takes two optional parameters. One is an int and the other is a string. Below is the definition of the Web Method: public...
0
by: =?Utf-8?B?a3Jpc2huYQ==?= | last post by:
Hi i am getting the following error while using XMLSerializer XmlSerializer ser = new XmlSerializer(typeof(Person)); ERROR: Unable to generate a temporary class (result=1). error CS0266: Cannot...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.