473,395 Members | 1,677 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,395 software developers and data experts.

Consuming WebService which returns NULL-values

Hello,

I'd like to consume a WebService, which returns an array of objects which
include several members of type System.String, System.Decimal and
System.DateTime.
In the WSDL-file, the members of the object are marked as nilable.
I generated the client classes using VS.NET 2003. After the creation, I got
the class-definition of the objects returned by the WebService too. BUT,
only the System.String members where marked to be nullable, System.Decimal
and System.DateTime don't. Ok, I know, they are value-types and cannot be
NULL, but thats exactly my problem.
If I then call the service, and it returns, I get an System.InvalidOperation
exception.
If I change the datatypes of the decimal and datetime-members to
System.Object, everything works, but I have to do the value-conversion
myself.

Is there a better way for solving that problem? I don't want to edit the
generated files everytime I regenerate them.

Thanks very much!
Max
Nov 21 '05 #1
6 12104
There is a convention in .NET XML Serialization (which is used by the
webservices runtime) that allows value-types to be marked as "nil".
The convention is, for each property in a class with a name "propertyName",
if there is a companion property named "propertyNameSpecified" which is a
bool, then the propertyNameSpecified indicates whether the property of name
"propertyName" is nil or not.

See
http://msdn.microsoft.com/library/en...ClassTopic.asp
for the doc on this.

For example, this complexType in XML Schema:
<s:complexType name="IdType">
<s:sequence>
<s:element name="Name" minOccurs="0" maxOccurs="1"
type="s:string" nillable="true" />
<s:element name="Stamp" minOccurs="0" maxOccurs="1"
type="s:date" nillable="true" />
<s:element name="Id" minOccurs="0" maxOccurs="1"
type="s:int" nillable="true" />
</s:sequence>
</s:complexType>

will generate something like this type definition in C#:

[XmlType(Namespace="urn:myNameSpaceHere")]
public class IdType {
[XmlElement(IsNullable=true)] public string Name;

[XmlElement(DataType="date")] public System.DateTime Stamp;
[XmlIgnore] public bool StampSpecified;

[XmlElement] public int Id;
[XmlIgnore] public bool IdSpecified;
}

---------

Notice that the string (not a value type) is marked IsNillable=true, while
the int and DateTime (both value types) are not. In your app code, to
deal with nil values in the string, you do this:

if (instance.Name != null) ...

Conversely, to deal with nil values on value types (like the int and
DateTime), what you need to do is

if (instance.StampSpecified) ....
if (instance.IdSpecified) ....

The Id and Stamp will never actually be nil, but you treat them as nil in
app code if the flag says that they are not specified. The same convention
applies to all value types in .NET.

--------

So, can you examine the generated files and tell me if you have the
companion xxxSpecified properties marked "XmlIgnore"? If so, then you
know what to do. . .

-Dino


"Markus Eßmayr" <essmayr/at/racon-linz.at> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hello,

I'd like to consume a WebService, which returns an array of objects which
include several members of type System.String, System.Decimal and
System.DateTime.
In the WSDL-file, the members of the object are marked as nilable.
I generated the client classes using VS.NET 2003. After the creation, I got the class-definition of the objects returned by the WebService too. BUT,
only the System.String members where marked to be nullable, System.Decimal
and System.DateTime don't. Ok, I know, they are value-types and cannot be
NULL, but thats exactly my problem.
If I then call the service, and it returns, I get an System.InvalidOperation exception.
If I change the datatypes of the decimal and datetime-members to
System.Object, everything works, but I have to do the value-conversion
myself.

Is there a better way for solving that problem? I don't want to edit the
generated files everytime I regenerate them.

Thanks very much!
Max

Nov 21 '05 #2
Dino,

thanks for your reply!

We now added the "minOccurs" and "maxOccurs" settings into our WSDL-file and
updated the server- and client-classes. Now it works fine because it just
doesn't send the NULL-values to the client.
Previously we didn't specify these two attributes.

But, what if I absolutely don't have a chance to modify the server-WSDL?
Let's say that the attributes are specified like this:
<s:element name="Stamp" minOccurs="1" maxOccurs="1" type="s:date"
nillable="true" />

So I won't have a chance to consume this service the "easy" way!

I wonder why there is no functionality in the .NET serialization, which
supports something like
[XmlIgnore] public bool StampIsNull;

What do you think?

Max
"Dino Chiesa [Microsoft]" <di****@online.microsoft.com> wrote in message
news:uJ**************@TK2MSFTNGP09.phx.gbl...
There is a convention in .NET XML Serialization (which is used by the
webservices runtime) that allows value-types to be marked as "nil".
The convention is, for each property in a class with a name "propertyName", if there is a companion property named "propertyNameSpecified" which is a
bool, then the propertyNameSpecified indicates whether the property of name "propertyName" is nil or not.

See
http://msdn.microsoft.com/library/en...ClassTopic.asp for the doc on this.

For example, this complexType in XML Schema:
<s:complexType name="IdType">
<s:sequence>
<s:element name="Name" minOccurs="0" maxOccurs="1"
type="s:string" nillable="true" />
<s:element name="Stamp" minOccurs="0" maxOccurs="1"
type="s:date" nillable="true" />
<s:element name="Id" minOccurs="0" maxOccurs="1"
type="s:int" nillable="true" />
</s:sequence>
</s:complexType>

will generate something like this type definition in C#:

[XmlType(Namespace="urn:myNameSpaceHere")]
public class IdType {
[XmlElement(IsNullable=true)] public string Name;

[XmlElement(DataType="date")] public System.DateTime Stamp;
[XmlIgnore] public bool StampSpecified;

[XmlElement] public int Id;
[XmlIgnore] public bool IdSpecified;
}

---------

Notice that the string (not a value type) is marked IsNillable=true, while
the int and DateTime (both value types) are not. In your app code, to
deal with nil values in the string, you do this:

if (instance.Name != null) ...

Conversely, to deal with nil values on value types (like the int and
DateTime), what you need to do is

if (instance.StampSpecified) ....
if (instance.IdSpecified) ....

The Id and Stamp will never actually be nil, but you treat them as nil in
app code if the flag says that they are not specified. The same convention applies to all value types in .NET.

--------

So, can you examine the generated files and tell me if you have the
companion xxxSpecified properties marked "XmlIgnore"? If so, then you
know what to do. . .

-Dino


"Markus Eßmayr" <essmayr/at/racon-linz.at> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hello,

I'd like to consume a WebService, which returns an array of objects which include several members of type System.String, System.Decimal and
System.DateTime.
In the WSDL-file, the members of the object are marked as nilable.
I generated the client classes using VS.NET 2003. After the creation, I

got
the class-definition of the objects returned by the WebService too. BUT,
only the System.String members where marked to be nullable, System.Decimal and System.DateTime don't. Ok, I know, they are value-types and cannot be NULL, but thats exactly my problem.
If I then call the service, and it returns, I get an

System.InvalidOperation
exception.
If I change the datatypes of the decimal and datetime-members to
System.Object, everything works, but I have to do the value-conversion
myself.

Is there a better way for solving that problem? I don't want to edit the
generated files everytime I regenerate them.

Thanks very much!
Max


Nov 21 '05 #3

"Markus Eßmayr" <essmayr/at/racon-linz.at> wrote in message
news:uq**************@tk2msftngp13.phx.gbl...
But, what if I absolutely don't have a chance to modify the server-WSDL?
Let's say that the attributes are specified like this:
<s:element name="Stamp" minOccurs="1" maxOccurs="1" type="s:date" nillable="true" />
So I won't have a chance to consume this service the "easy" way!

Let's not discuss whether it actually makes sense to have a minOccurs=1
element be nillable. Suppose that it does make sense.
Generating a proxy class from this WSDL will NOT generate the StampSpecified
bool property.

This seems broken. However, you can easily add it in yourself. (I know you
said you did not want to modify the generated files, but the point is you
could do so)

The other option is to modify the server-side WSDL before you use it to
generate code. I often do this to rename artifacts or fixup things that I
know, through testing, are broken. For example, a service is sending an
xsd:date, but the WSDL specifies an xsd:dateTime. And so on. So you
could just snapshot the WSDL and tweak it a bit.

I wonder why there is no functionality in the .NET serialization, which
supports something like
[XmlIgnore] public bool StampIsNull;

What do you think?


How would this be different from the StampSpecified mechanism that is
currently supported ?

-D
Nov 21 '05 #4
Please see below ...

Max

"Dino Chiesa [Microsoft]" <di****@online.microsoft.com> wrote in message
news:uW*************@TK2MSFTNGP12.phx.gbl...

"Markus Eßmayr" <essmayr/at/racon-linz.at> wrote in message
news:uq**************@tk2msftngp13.phx.gbl...
But, what if I absolutely don't have a chance to modify the server-WSDL?
Let's say that the attributes are specified like this:
<s:element name="Stamp" minOccurs="1" maxOccurs="1" type="s:date" nillable="true" />

So I won't have a chance to consume this service the "easy" way!


Let's not discuss whether it actually makes sense to have a minOccurs=1
element be nillable. Suppose that it does make sense.
Generating a proxy class from this WSDL will NOT generate the

StampSpecified bool property.

This seems broken. However, you can easily add it in yourself. (I know you said you did not want to modify the generated files, but the point is you
could do so)

The other option is to modify the server-side WSDL before you use it to
generate code. I often do this to rename artifacts or fixup things that I
know, through testing, are broken. For example, a service is sending an
xsd:date, but the WSDL specifies an xsd:dateTime. And so on. So you
could just snapshot the WSDL and tweak it a bit.

I wonder why there is no functionality in the .NET serialization, which
supports something like
[XmlIgnore] public bool StampIsNull;

What do you think?
How would this be different from the StampSpecified mechanism that is
currently supported ?


The problem is: If I'm not able to make changes to the server side WSDL
(if I use a service offered by a 3rd party company, which doesn't want to
modify it and more), and they have minOccurs="1" (or lets say, they just
forgot to make the minOccurs definition, which I think defaults to 1),
they send the fields containing a null value. Now, AFAIK, .NET is NOT
able to consume this service! Even that NULL values are included in the
XML specification, it won't be possible to consume that service, because
the field does come down from the server (StampSpecified=true), BUT
it contains NULL, which cannot be mapped to any System.DateTime.
Here a StampIsNull=true would help a lot.

But I just read about the nullable types in .NET 2.0. Maybe there is some
solution for this!


-D

Nov 21 '05 #5

"Markus Eßmayr" <essmayr/at/racon-linz.at> wrote in message
news:u0**************@TK2MSFTNGP09.phx.gbl...
Please see below ... The problem is: If I'm not able to make changes to the server side WSDL
(if I use a service offered by a 3rd party company, which doesn't want to
modify it and more), and they have minOccurs="1" (or lets say, they just
forgot to make the minOccurs definition, which I think defaults to 1),
they send the fields containing a null value. Now, AFAIK, .NET is NOT
able to consume this service! Even that NULL values are included in the
XML specification, it won't be possible to consume that service, because
the field does come down from the server (StampSpecified=true), BUT
it contains NULL, which cannot be mapped to any System.DateTime.
Here a StampIsNull=true would help a lot.

Ahh, ok, I see. Yes, you are correct.
You are getting from the server
<Stamp isNil="true" />

in which case, the XML Serializer chokes. So you would need to do some
fancy footwork on the client side in order to de-serialize a nil DateTime.
But I just read about the nullable types in .NET 2.0. Maybe there is some
solution for this!

yes, in 2.0.
In v1.1, I think you have work to do. It is not easy, but it is possible.

This is the default definition for a <xsd:dateTime> .

public class MyType {
public System.DateTime DateTimeField;
}

If you modify it to something like this, you may get the behavior you want:
public class MyType {
private static string formatString= "yyyy-MM-ddTHH:mm:ss.fffffffzzz";
private static System.Globalization.CultureInfo CInfo= new
System.Globalization.CultureInfo("en-US", true);

[XmlIgnore] public System.DateTime internal_DateTimeField;
[XmlIgnore] public bool DateTimeFieldIsNull; // ideally this would be
provided by .NET, but it isn't.

[XmlElement(IsNullable=true)]
public string DateTimeField {
set {
if ((value!=null) && (value != "")) {
internal_DateTimeField= System.DateTime.ParseExact(value,
formatString, CInfo);
DateTimeFieldIsNull= false;
}
else
DateTimeFieldIsNull= true;
}
get {
return (DateTimeFieldIsNull) ?
null :
internal_DateTimeField.ToString(formatString) ;
}
}
}
-D
Nov 21 '05 #6
Thanks very much for your answer.
I'll try to use it somehow, but it'll take a while! ;-)

Max

"Dino Chiesa [Microsoft]" <di****@online.microsoft.com> wrote in message
news:eL**************@TK2MSFTNGP12.phx.gbl...

"Markus Eßmayr" <essmayr/at/racon-linz.at> wrote in message
news:u0**************@TK2MSFTNGP09.phx.gbl...
Please see below ...
The problem is: If I'm not able to make changes to the server side WSDL
(if I use a service offered by a 3rd party company, which doesn't want to modify it and more), and they have minOccurs="1" (or lets say, they just
forgot to make the minOccurs definition, which I think defaults to 1),
they send the fields containing a null value. Now, AFAIK, .NET is NOT
able to consume this service! Even that NULL values are included in the
XML specification, it won't be possible to consume that service, because
the field does come down from the server (StampSpecified=true), BUT
it contains NULL, which cannot be mapped to any System.DateTime.
Here a StampIsNull=true would help a lot.


Ahh, ok, I see. Yes, you are correct.
You are getting from the server
<Stamp isNil="true" />

in which case, the XML Serializer chokes. So you would need to do some
fancy footwork on the client side in order to de-serialize a nil DateTime.
But I just read about the nullable types in .NET 2.0. Maybe there is some solution for this!

yes, in 2.0.
In v1.1, I think you have work to do. It is not easy, but it is

possible.
This is the default definition for a <xsd:dateTime> .

public class MyType {
public System.DateTime DateTimeField;
}

If you modify it to something like this, you may get the behavior you want: public class MyType {
private static string formatString= "yyyy-MM-ddTHH:mm:ss.fffffffzzz";
private static System.Globalization.CultureInfo CInfo= new
System.Globalization.CultureInfo("en-US", true);

[XmlIgnore] public System.DateTime internal_DateTimeField;
[XmlIgnore] public bool DateTimeFieldIsNull; // ideally this would be provided by .NET, but it isn't.

[XmlElement(IsNullable=true)]
public string DateTimeField {
set {
if ((value!=null) && (value != "")) {
internal_DateTimeField= System.DateTime.ParseExact(value,
formatString, CInfo);
DateTimeFieldIsNull= false;
}
else
DateTimeFieldIsNull= true;
}
get {
return (DateTimeFieldIsNull) ?
null :
internal_DateTimeField.ToString(formatString) ;
}
}
}
-D

Nov 21 '05 #7

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

Similar topics

2
by: Rene van Hoek | last post by:
Hi, I am using Xalan 1.8.0 and Xerces 2.6.0 in C++. I have an XML document which I first transform into an other XML document using an XSL styelsheet. Then I want to parse with XPathEvaluator...
1
by: Suresh | last post by:
Hi, I have an C# CSEXE.exe (CSexe.cs) and a CSDll.dll (CSdll.cs). exe is compiled with a reference to dll. Calling for the class Type defined in CSDLL.dll using Type.GetType(...
2
by: Jeff Adams | last post by:
I am using MSVC .NET to create a C program. I am having trouble creating a window. The createwindow returns NULL however no error is caught. The GetLastError() returns "operation completed...
3
by: Zoury | last post by:
Good day! :O) I have the following code : //*** using System; using System.Windows.Forms; <snip>
8
by: Brian | last post by:
This is causing me to not be able to create a relation in my dataset. Here's my code: dc1 = ds.Tables .Columns ; dc2 = ds.Tables .Columns ; dr1 = new System.Data.DataRelation...
5
by: mtv | last post by:
Hi all, I have the following code: ================================ Webservice side: public class MyWS: WebService { private myLib.DataObject curDataObject;
2
by: Beowulf | last post by:
If I run this statement in Query Analyzer, it properly returns 1 for my testing table. But if I put the statement into a stored procedure, the stored procedure returns NULL. What am I doing...
0
by: Zean Smith | last post by:
I am trying to get my C# client to connect to a 3rd Perl Web Service (SOAP Lite), this is the code that fails ...... object results = this.Invoke("getGrossData", new object {week} ); return...
11
by: MLH | last post by:
I have 2 lines in a procedure that assign MyVariant a value - line #238 and line #491. When line #238 runs, the value is 152. When line #491 runs, the DLookup function returns Null. I would expect...
6
by: Peter Michaux | last post by:
Suppose I have implemented a language with garbage collection in C. I have wrapped malloc in my own C function. If malloc returns NULL then I can run the garbage collector and then try malloc...
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
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.