473,327 Members | 2,081 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,327 software developers and data experts.

How do you ensure that null-valued elements are not serialized?

I am using complex types in order to support serialization/deserialization
of floating point numbers, since floating points can't be null.
I've seen how to suppress attributes that are "not specified", such as
having a float member called Value, and a bool member called ValueSpecified.
This instructs the XML Serializer to omit that attribute altogether if it
wasn't "Specified". But how can I tell it to omit the XML element
altogether?
Here's the problem:
I deserialize an object that looks like this:
<person>
<name>Joe</name>
<age>37</age>
</person>
When deserialized, I get an object of type person, with a field age of type
floatType which has Value = 37 and ValueSpecified = false.
If I change name to "Bob" (p.name = "Bob"), and then immediately serialize
that object to XML, I get:
<person>
<name>Bob</name>
<age />
</person>
This indicates that age should now be null (if I pass this off to a SOAP
server for instance), or if that value is not allowed to be nulled, then I
get a SOAP exception.
If I explicitly set p.age.ValueSpecified = true or set p.age.Value = 36
(which implicitly sets ValueSpecified to true)
Then I get the expected
<person>
<name>Bob</name>
<age>36</age>
</person>

What I would like to see happen is if ValueSpecified is false, then just
leave that element off altogether. Is there a way to do this?
Here's what the generated code for the objects looks like:

[XmlType(TypeName="floatType",Namespace=Declaration s.SchemaVersion),XmlRoot,Serializable]

[EditorBrowsable(EditorBrowsableState.Advanced)]

public class floatType

{

[XmlAttribute(AttributeName="value",Form=XmlSchemaF orm.Unqualified,DataType="string",Namespace=Declar ations.SchemaVersion)]

[EditorBrowsable(EditorBrowsableState.Advanced)]

public string __value;
[XmlIgnore]

public string @value

{

get { return __value; }

set { __value = value; }

}

[XmlText(DataType="float")]

[EditorBrowsable(EditorBrowsableState.Advanced)]

public float __Value;
[XmlIgnore]

[EditorBrowsable(EditorBrowsableState.Advanced)]

public bool __ValueSpecified;
[XmlIgnore]

public float Value

{

get { return __Value; }

set { __Value = value; __ValueSpecified = true; }

}

public floatType()

{

}

public void MakeSchemaCompliant()

{

}

public static event DepthFirstTraversalDelegate DepthFirstTraversalEvent;

public void DepthFirstTraversal(object parent, object context)

{

if (DepthFirstTraversalEvent != null) DepthFirstTraversalEvent(this, parent,
context);

}

}

Here's the XSD that the code was generated from:

<xsd:complexType name="floatType">
<xsd:simpleContent>
<xsd:extension base="xsd:float">
<xsd:attribute name="value" type="xsd:string"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>


Thanks for your help


Nov 12 '05 #1
0 2072

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

Similar topics

3
by: Paul Reddin | last post by:
Hi, We have a whole set of Views we would like to structure a follows, but they don't seem to optimise in the way we would like: e.g CREATE VIEW1 (col1,col2,col3....) as (
4
by: Konstantinos | last post by:
I cant find much info or a code on how to do this. I have a generic table called Contact. Then I have tables Person and Company that are subtypes of contact each with attributes that are specific...
2
by: Rene_Muench | last post by:
Hi there, I have listed 12 month on a continous form, where as I flag the current month with a tick box. To ensure at only 1 tick box is check not more or all I would want to validate and...
5
by: Dianne Siebold | last post by:
I'm not sure if I can use regex for this or how to do it, but here's what I need to do: I want to check that a string contains only the characters A-Z, a-z, 0-9, -(hypen) or _ (underscore). If...
8
by: Asha | last post by:
greetings, besides using objconn.State() to find out if a sql connection is close or open, is there any other way to accomplish this task? thanks in advance. anywhere is sql server or the machine...
3
by: Ray Cassick \(Home\) | last post by:
I have several classes that has a public interface (nothing really different there :) ). 1) I would like to ensure that some classes can only be instantiated by a specific class type. 2) ...
1
by: terry.statham | last post by:
Hope someone can help me here. I am fairly new to Access. I have set up a database relating to vehicle movements prior to sale. I have a subform based on 'locations' table which is linked to my...
2
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
1
by: Dhruba Bandopadhyay | last post by:
Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm....
7
by: David | last post by:
Hi using C#, .NET 1.1 I want to ensure a particular control is in a certain place in the page... For example, in the head section, I want to ensure that a literal control of a certain name...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.