473,651 Members | 3,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deserialization of Null Elements

I have some XML that contains some null elements, for example:

<Document>
<StartDate/>
<EndDate/>
</Document>

I would like to process the XML using the .NET serialization tools
(XSD.exe). The schema for the XML is under external control (i.e. I can't
change it).

As far as I can tell there is no way to detect the presence of null elements
(like StartDate and EndDate above) in deserialized objects (i.e. the
generated objects are identical if the elements are null or if they are not
present).

Am I correct in my understanding of this? Can you recommend an approach for
solving this problem?
May 15 '06 #1
7 2168
Hi Jason,

Yes, when deserialized to an object, the results are the same if the
element does not exist or it has no value. Could you let me know why you
need to distinguish between no value and no element? As far as I know, we
have to parse the xml manually to achieve this.

Kevin Yu
Microsoft Online Community Support

=============== =============== =============== =============== =============== =
=============== ===========
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =============== =============== =
=============== ===========

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

May 16 '06 #2
Hi Kevin,

Thanks for the response.

The xml we are dealing with is being used as a kind of query template for an
application. The null elements identify data that should be provided by our
application which is the recipient of this XML. I guess the problem could be
easily solved by modifying the XML by adding attributes to the empty elements
- but the problem is that the XML is being defined externally - we do not
have control over the format.

I guessed that maybe the best way around this would be to pre-process the
XML to identify the null elements before deserializing it. Is this the kind
of approach you would recommend?

Jason

"Kevin Yu [MSFT]" wrote:
Hi Jason,

Yes, when deserialized to an object, the results are the same if the
element does not exist or it has no value. Could you let me know why you
need to distinguish between no value and no element? As far as I know, we
have to parse the xml manually to achieve this.

Kevin Yu
Microsoft Online Community Support

=============== =============== =============== =============== =============== =
=============== ===========
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =============== =============== =
=============== ===========

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

May 16 '06 #3
Does the schema support use of xsi:nil? If so you can use

[XmlElement(IsNu llable=true)]

in your class and get a null reference rather than an empty string.

=============== =======
Clive Dixon
Digita Ltd. (www.digita.com)
"Jason" <Ja***@discussi ons.microsoft.c om> wrote in message
news:01******** *************** ***********@mic rosoft.com...
Hi Kevin,

Thanks for the response.

The xml we are dealing with is being used as a kind of query template for
an
application. The null elements identify data that should be provided by
our
application which is the recipient of this XML. I guess the problem could
be
easily solved by modifying the XML by adding attributes to the empty
elements
- but the problem is that the XML is being defined externally - we do not
have control over the format.

I guessed that maybe the best way around this would be to pre-process the
XML to identify the null elements before deserializing it. Is this the
kind
of approach you would recommend?

Jason

"Kevin Yu [MSFT]" wrote:
Hi Jason,

Yes, when deserialized to an object, the results are the same if the
element does not exist or it has no value. Could you let me know why you
need to distinguish between no value and no element? As far as I know, we
have to parse the xml manually to achieve this.

Kevin Yu
Microsoft Online Community Support

=============== =============== =============== =============== =============== =
=============== ===========
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =============== =============== =
=============== ===========

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

May 16 '06 #4
Thanks for the response.

The problem is that we don't own the schema - so we can't change it ourselves.

I think that the solution has to be to parse the document with using the
other XML tools?

"Clive Dixon" wrote:
Does the schema support use of xsi:nil? If so you can use

[XmlElement(IsNu llable=true)]

in your class and get a null reference rather than an empty string.

=============== =======
Clive Dixon
Digita Ltd. (www.digita.com)
"Jason" <Ja***@discussi ons.microsoft.c om> wrote in message
news:01******** *************** ***********@mic rosoft.com...
Hi Kevin,

Thanks for the response.

The xml we are dealing with is being used as a kind of query template for
an
application. The null elements identify data that should be provided by
our
application which is the recipient of this XML. I guess the problem could
be
easily solved by modifying the XML by adding attributes to the empty
elements
- but the problem is that the XML is being defined externally - we do not
have control over the format.

I guessed that maybe the best way around this would be to pre-process the
XML to identify the null elements before deserializing it. Is this the
kind
of approach you would recommend?

Jason

"Kevin Yu [MSFT]" wrote:
Hi Jason,

Yes, when deserialized to an object, the results are the same if the
element does not exist or it has no value. Could you let me know why you
need to distinguish between no value and no element? As far as I know, we
have to parse the xml manually to achieve this.

Kevin Yu
Microsoft Online Community Support

=============== =============== =============== =============== =============== =
=============== ===========
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =============== =============== =
=============== ===========

(This posting is provided "AS IS", with no warranties, and confers no
rights.)


May 17 '06 #5
I'm not actually sure if xsi:nil is something you need to permit in the
schema itself or whether you can just go ahead use it in XML anyway, though
I don't really know about these things.

Your own processing seems like the only way if the data cannot contain
xsi:nil attributes, though I would think you are breaking W3C in doing this
which I think expects <Element/> and <Element></Element> to be treated
identically.

=============== =======
Clive Dixon
Digita Ltd. (www.digita.com)
"Jason" <Ja***@discussi ons.microsoft.c om> wrote in message
news:CD******** *************** ***********@mic rosoft.com...
Thanks for the response.

The problem is that we don't own the schema - so we can't change it
ourselves.

I think that the solution has to be to parse the document with using the
other XML tools?

"Clive Dixon" wrote:
Does the schema support use of xsi:nil? If so you can use

[XmlElement(IsNu llable=true)]

in your class and get a null reference rather than an empty string.

=============== =======
Clive Dixon
Digita Ltd. (www.digita.com)
"Jason" <Ja***@discussi ons.microsoft.c om> wrote in message
news:01******** *************** ***********@mic rosoft.com...
> Hi Kevin,
>
> Thanks for the response.
>
> The xml we are dealing with is being used as a kind of query template
> for
> an
> application. The null elements identify data that should be provided by
> our
> application which is the recipient of this XML. I guess the problem
> could
> be
> easily solved by modifying the XML by adding attributes to the empty
> elements
> - but the problem is that the XML is being defined externally - we do
> not
> have control over the format.
>
> I guessed that maybe the best way around this would be to pre-process
> the
> XML to identify the null elements before deserializing it. Is this
> the
> kind
> of approach you would recommend?
>
> Jason
>
> "Kevin Yu [MSFT]" wrote:
>
>> Hi Jason,
>>
>> Yes, when deserialized to an object, the results are the same if the
>> element does not exist or it has no value. Could you let me know why
>> you
>> need to distinguish between no value and no element? As far as I know,
>> we
>> have to parse the xml manually to achieve this.
>>
>> Kevin Yu
>> Microsoft Online Community Support
>>
>> =============== =============== =============== =============== =============== =
>> =============== ===========
>> When responding to posts, please "Reply to Group" via your newsreader
>> so
>> that others may learn and benefit from your issue.
>> =============== =============== =============== =============== =============== =
>> =============== ===========
>>
>> (This posting is provided "AS IS", with no warranties, and confers no
>> rights.)
>>
>>


May 17 '06 #6
Oh - it is not the distinction between

<Element/> and <Element></Element>

we are interested in. It is purely the difference between the presence of
an element and its absence.
"Clive Dixon" wrote:
I'm not actually sure if xsi:nil is something you need to permit in the
schema itself or whether you can just go ahead use it in XML anyway, though
I don't really know about these things.

Your own processing seems like the only way if the data cannot contain
xsi:nil attributes, though I would think you are breaking W3C in doing this
which I think expects <Element/> and <Element></Element> to be treated
identically.

=============== =======
Clive Dixon
Digita Ltd. (www.digita.com)
"Jason" <Ja***@discussi ons.microsoft.c om> wrote in message
news:CD******** *************** ***********@mic rosoft.com...
Thanks for the response.

The problem is that we don't own the schema - so we can't change it
ourselves.

I think that the solution has to be to parse the document with using the
other XML tools?

"Clive Dixon" wrote:
Does the schema support use of xsi:nil? If so you can use

[XmlElement(IsNu llable=true)]

in your class and get a null reference rather than an empty string.

=============== =======
Clive Dixon
Digita Ltd. (www.digita.com)
"Jason" <Ja***@discussi ons.microsoft.c om> wrote in message
news:01******** *************** ***********@mic rosoft.com...
> Hi Kevin,
>
> Thanks for the response.
>
> The xml we are dealing with is being used as a kind of query template
> for
> an
> application. The null elements identify data that should be provided by
> our
> application which is the recipient of this XML. I guess the problem
> could
> be
> easily solved by modifying the XML by adding attributes to the empty
> elements
> - but the problem is that the XML is being defined externally - we do
> not
> have control over the format.
>
> I guessed that maybe the best way around this would be to pre-process
> the
> XML to identify the null elements before deserializing it. Is this
> the
> kind
> of approach you would recommend?
>
> Jason
>
> "Kevin Yu [MSFT]" wrote:
>
>> Hi Jason,
>>
>> Yes, when deserialized to an object, the results are the same if the
>> element does not exist or it has no value. Could you let me know why
>> you
>> need to distinguish between no value and no element? As far as I know,
>> we
>> have to parse the xml manually to achieve this.
>>
>> Kevin Yu
>> Microsoft Online Community Support
>>
>> =============== =============== =============== =============== =============== =
>> =============== ===========
>> When responding to posts, please "Reply to Group" via your newsreader
>> so
>> that others may learn and benefit from your issue.
>> =============== =============== =============== =============== =============== =
>> =============== ===========
>>
>> (This posting is provided "AS IS", with no warranties, and confers no
>> rights.)
>>
>>


May 17 '06 #7
Hi Jason,

This behavior really depends on the deserialized object. If the memeber is
a string, absence means it's a null reference and empty means it's an empty
string during deseiralization . But for DateTime, it's a little difference,
and in this case, I think you have to do something with the raw xml, not
the serializer.

Kevin Yu
Microsoft Online Community Support

=============== =============== =============== =============== =============== =
=============== ===========
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =============== =============== =
=============== ===========

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

May 19 '06 #8

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

Similar topics

3
9792
by: Amadelle | last post by:
Hi all and thanks in advance for your help, I am having problems deserializing an object which seems to be serializing just fine. I save the byte array of the serialized object in the database (when I check the database the field that holds the binary data seems populated), but when I want to deserialize this same byte array the application fails and gives me the following error: Binary stream does not contain a valid BinaryHeader, 0...
1
6595
by: Taiwo | last post by:
I generated a Typed Dataset class including a base64Binary column. This column was specified as a .NET type of Byte() in the class that was auto-generated. I set the value of this property to New Byte() {} (i.e. Byte array with zero elements) in a row of an instance of the class prior to serialization using the binary formatter. When I attempt to DeSerialize the instance, the following exception is thrown: :...
3
9791
by: parrot toes | last post by:
Summary: I have been trying to make requests of a web service provided by Axis using a dotnet client with code generated by wsdl.exe and have been getting exceptions when trying to process the response. As a result of seraching news groups I guessed that the SOAP response defines an array element in a way that causes the dotnet deserialization routines to put the content in a generic object array (object) BUT the content is supposed to...
1
3731
by: parrot toes | last post by:
I tried to post this question before, but there was an error when posting. I case it did get posted and in order to avoid duplication, I'll just repost a summary. I have written a dotnet client that accesses a Axis provided web service. The client uses the code generated, using wsdl.exe, from the wsdl file provided by the web service provider. The web response has the following schema (roughly):
5
5528
by: frustratedWithDotNet | last post by:
Why does .NET not issue messages or throw exceptions if it doesn't like something in the response from a web service?? I am getting a response object, but an array of custom objects within the response is null instead of being populated. The SOAP response from the service looks good and I cannot see anything wrong with the WSDL or XML schema. How do I get .NET to tell me what it doesn't like? Is there a way to turn on some kind of tracing...
5
2289
by: Greg Allen | last post by:
I am consuming a web service and using the generated Reference.cs to access the service and the objects associated with it. I have run into a problem where some inherited classes are not being deserialized. I have verified that the XML being returned by the service contains the tags I am expecting, but they don't show up in the resulting object. Here's the relevant portion of the Reference.cs file: public class FormSection {
6
2078
by: Marcus | last post by:
I have a couple of classes looking like this: ======================================== public class AdminSettingsData { public string adminSettingsPath; public string adminSettingsFile; public string usersSettingsPath;
3
4989
by: John Glover | last post by:
To whoever can help, I've been having a problem with XML deserialization lately. I needed to serialize and deserialze two objects which inherited from Hashtable. Because IDictionary implementations cannot be serialized, I had to take matters into my own hands by implementing a wrapper over the Hashtable which implemted IXmlSerializable. I called it XmlHashtable. It has, among other convenience methods, a method
6
2496
by: beachdog | last post by:
I am building a client which accesses a web service (HTTP/SOAP), and I think the response message is somehow not in agreement with my wsdl/ proxy class. I am able to access SOAP headers ok from the deserialized response, but not elements in the body of the response. Is there some way I can get some sort of debug tracing out of the deserialization layer, or step thru it in debugger somehow, so I can see how it is parsing the message and...
0
8278
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8807
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8701
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8584
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7299
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5615
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.