473,586 Members | 2,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XMLSerializer

I'm using XMLSerializer to serialize hierarchical Class-based objects and
their content to XML. There are a number of fields (i.e. properties) that
are optional, meaning that if no data id written to that property of the
object, then no entry is to appear in the XML, as the destination system
already knows the default values for those fields.

The issue I'm having is that although this is easy to do with String
properties by setting them equal to nothing in the definition, this method
doesn't work for other property types, such as Integer and Boolean. If I
set Integer and Boolean properties to nothing in the definition, they are
still serialized to XML, as 0 and False respectively. If I want those
fields to default to not appear in the XML by default, I seem to be forced
to replace those properties with String properties.

Is there any way to do this with Integer and Boolean properties? Thanks.
Nov 16 '06 #1
5 2942
Phil Galey wrote:
I'm using XMLSerializer to serialize hierarchical Class-based objects and
their content to XML. There are a number of fields (i.e. properties) that
are optional, meaning that if no data id written to that property of the
object, then no entry is to appear in the XML, as the destination system
already knows the default values for those fields.

The issue I'm having is that although this is easy to do with String
properties by setting them equal to nothing in the definition, this method
doesn't work for other property types, such as Integer and Boolean. If I
set Integer and Boolean properties to nothing in the definition, they are
still serialized to XML, as 0 and False respectively. If I want those
fields to default to not appear in the XML by default, I seem to be forced
to replace those properties with String properties.

Is there any way to do this with Integer and Boolean properties?
I think the difference is that Integer and Boolean are value types while
String is a reference type that you can set to Nothing (or null for C#).

What you can do is use Nullable types in .NET 2.0 but then if the
property is null (or Nothing) the serializer will serialize as e.g.
<Property-name xsi:nil="true" />
so the XML will not have any value but rather the special xsi:nil
attribute with value true to indicate the value is nil (which is the XSD
equivalent of null or Nothing).
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 16 '06 #2
Can't you just set the defaultvalueatt ribute to the appropiate value?


Phil Galey wrote:
I'm using XMLSerializer to serialize hierarchical Class-based objects and
their content to XML. There are a number of fields (i.e. properties) that
are optional, meaning that if no data id written to that property of the
object, then no entry is to appear in the XML, as the destination system
already knows the default values for those fields.

The issue I'm having is that although this is easy to do with String
properties by setting them equal to nothing in the definition, this method
doesn't work for other property types, such as Integer and Boolean. If I
set Integer and Boolean properties to nothing in the definition, they are
still serialized to XML, as 0 and False respectively. If I want those
fields to default to not appear in the XML by default, I seem to be forced
to replace those properties with String properties.

Is there any way to do this with Integer and Boolean properties? Thanks.
Nov 16 '06 #3
Can't you just set the defaultvalueatt ribute to the appropiate value?


Phil Galey wrote:
I'm using XMLSerializer to serialize hierarchical Class-based objects and
their content to XML. There are a number of fields (i.e. properties) that
are optional, meaning that if no data id written to that property of the
object, then no entry is to appear in the XML, as the destination system
already knows the default values for those fields.

The issue I'm having is that although this is easy to do with String
properties by setting them equal to nothing in the definition, this method
doesn't work for other property types, such as Integer and Boolean. If I
set Integer and Boolean properties to nothing in the definition, they are
still serialized to XML, as 0 and False respectively. If I want those
fields to default to not appear in the XML by default, I seem to be forced
to replace those properties with String properties.

Is there any way to do this with Integer and Boolean properties? Thanks.
Nov 16 '06 #4
Yes, you're right.
Imports System.Componen tModel
and
<DefaultValue(< default value>)Public ....

does the trick. Thanks for your help.

Phillip
"kferron" <ke**********@g mail.comwrote in message news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
Can't you just set the defaultvalueatt ribute to the appropiate value?


Phil Galey wrote:
I'm using XMLSerializer to serialize hierarchical Class-based objects and
their content to XML. There are a number of fields (i.e. properties) that
are optional, meaning that if no data id written to that property of the
object, then no entry is to appear in the XML, as the destination system
already knows the default values for those fields.

The issue I'm having is that although this is easy to do with String
properties by setting them equal to nothing in the definition, this method
doesn't work for other property types, such as Integer and Boolean. If I
set Integer and Boolean properties to nothing in the definition, they are
still serialized to XML, as 0 and False respectively. If I want those
fields to default to not appear in the XML by default, I seem to be forced
to replace those properties with String properties.

Is there any way to do this with Integer and Boolean properties? Thanks.
Nov 17 '06 #5

cool yeah, i forgot to mention it wasn't in the serialization
namespace.. its kinda hidden, makes me think they should have made an
alias derivation just so you could see it as a serialization option, if
youre like those of use intellisense to see quickly the options we have
available to us.. anyway, cheers.. one note is i've found that it is
helpful to make sure i'm also initializing the values of the type as
well.. it may make sense to use consts if you're a member of that 'only
hardcode in one place' school of thought.
~kcf

Phil Galey wrote:
Yes, you're right.
Imports System.Componen tModel
and
<DefaultValue(< default value>)Public ....

does the trick. Thanks for your help.

Phillip
"kferron" <ke**********@g mail.comwrote in message news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
Can't you just set the defaultvalueatt ribute to the appropiate value?


Phil Galey wrote:
I'm using XMLSerializer to serialize hierarchical Class-based objects and
their content to XML. There are a number of fields (i.e. properties) that
are optional, meaning that if no data id written to that property of the
object, then no entry is to appear in the XML, as the destination system
already knows the default values for those fields.
>
The issue I'm having is that although this is easy to do with String
properties by setting them equal to nothing in the definition, this method
doesn't work for other property types, such as Integer and Boolean. If I
set Integer and Boolean properties to nothing in the definition, they are
still serialized to XML, as 0 and False respectively. If I want those
fields to default to not appear in the XML by default, I seem to be forced
to replace those properties with String properties.
>
Is there any way to do this with Integer and Boolean properties? Thanks.

------=_NextPart_000_ 001E_01C7099D.F 46C1D30
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
X-Google-AttachSize: 2623

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2600.0" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=Arial size=2>Yes, you're right.</FONT></DIV>
<DIV>
<P><FONT face=Arial size=2><FONT color=#000080>I mports
System.Componen tModel</FONT><BR>and<BR ><FONT
color=#000080>& lt;DefaultValue (&lt;default value&gt;)&gt; Public
....</FONT></FONT></P>
<P><FONT face=Arial size=2>does the trick.&nbsp; Thanks for your
help.</FONT></P></DIV>
<DIV><FONT face=Arial size=2>Phillip</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>"kferron " &lt;</FONT><A
href="mailto:ke **********@gmai l.com"><FONT face=Arial
size=2>ke****** ****@gmail.com</FONT></A><FONT face=Arial size=2>&gt; wrote in
message </FONT><A
href="news:11** *************** *****@b28g2000c wb.googlegroups .com"><FONT
face=Arial
size=2>news:11* *************** ******@b28g2000 cwb.googlegroup s.com</FONT></A><FONT
face=Arial size=2>...</FONT></DIV><FONT face=Arial size=2>&gt; Can't you just
set the defaultvalueatt ribute to the appropiate value?<BR>&gt; <BR>&gt; <BR>&gt;
<BR>&gt; <BR>&gt; Phil Galey wrote:<BR>&gt; &gt; I'm using XMLSerializer to
serialize hierarchical Class-based objects and<BR>&gt; &gt; their content to
XML.&nbsp; There are a number of fields (i.e. properties) that<BR>&gt; &gt; are
optional, meaning that if no data id written to that property of the<BR>&gt;
&gt; object, then no entry is to appear in the XML, as the destination
system<BR>&gt; &gt; already knows the default values for those fields.<BR>&gt;
&gt;<BR>&gt; &gt; The issue I'm having is that although this is easy to do with
String<BR>&gt; &gt; properties by setting them equal to nothing in the
definition, this method<BR>&gt; &gt; doesn't work for other property types, such
as Integer and Boolean.&nbsp; If I<BR>&gt; &gt; set Integer and Boolean
properties to nothing in the definition, they are<BR>&gt; &gt; still serialized
to XML, as 0 and False respectively.&n bsp; If I want those<BR>&gt; &gt; fields
to default to not appear in the XML by default, I seem to be forced<BR>&gt; &gt;
to replace those properties with String properties.<BR> &gt; &gt;<BR>&gt; &gt; Is
there any way to do this with Integer and Boolean properties?&nbs p;
Thanks.<BR>&gt; </FONT></BODY></HTML>

------=_NextPart_000_ 001E_01C7099D.F 46C1D30--
Nov 17 '06 #6

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

Similar topics

4
1612
by: Zion Zadik | last post by:
Dear all, I have a set of c# data classes which i need to fill their data from xml files. serialization looks to be the best way to accomplish this task. Since the data classes are compiled and i don't have control on the xml structure, I tried using the xmlAttributeOverrides class, to instruct the serializer. I was able to override the...
5
5412
by: Stuart Robertson | last post by:
I am trying to find a solution that will allow me to use XmlSerializer to serialize/deserialize a collection of objects where a given object is shared between two or more other objects, and not create duplicate XML representations of the shared object, but instead use IDREFs to refer to the shared object. The XML I'm trying to produce is as...
1
4308
by: Bluetears76 | last post by:
Hi I have a hirachy of classes which are Message(base), then FileMessage and ChatMessage (extended) I want to serialize the objects and when i am deserizaling i dont know if i am getting FileMessage or ChatMessage. So how to get that object and use it I have written following code for serialization public void Send(Message message) {...
3
6991
by: Anthony Bouch | last post by:
Hi I've been reading using the XmlSerializer with custom collections. I've discovered that when serializing a custom collection (a class that implements ICollection, IList etc.) the XmlSerializer will only serialize the collection items - with the default root as ArrayofMyItems etc. My custom collection class has some additional public...
4
11364
by: Andy Neilson | last post by:
I've run across a strange behaviour with XmlSerializer that I'm unable to explain. I came across this while trying to use XmlSerializer to deserialize from a the details of a SoapException. This should have worked fine since the class in question was already being serialized and deserialized as part of a Web service interface. What I found was...
3
4503
by: Loui Mercieca | last post by:
Hi, I have created a class, named FormField , which basically contains two fields, name and value. I have set the tag before the class and the field is set as an XmlAttribute whil the name as XmlText. In my main class, i have created an arraylist which contains a collection of this class FormField. Basically its: public void Add(...
12
8472
by: SJD | last post by:
I've just read Christoph Schittko's article on XmlSerializer: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnxmlnet/html/trblshtxsd.asp . . . and very informative it is too. I, too, am getting nasty FileNotFound exceptions. I've read, and digested the article, and I think I've found a bug -- it's difficult to track,...
4
1984
by: Steve Long | last post by:
Hello, I hope this is the right group to post this to. I'm trying to serialize a class I've written and I'd like to be able to serialze to both binary and xml formats. Binary serialization is working fine but when I try to instantiate an XmlSerializer object with: Dim xmls As New XmlSerializer(GetType(CLayerDefinition)) I get the...
0
2287
by: William Stacey [MVP] | last post by:
Had a method that got some string info from mp3 tags in N files and serializes this class and deserializes at other side. Works ok except sometimes get chars that choke the XmlSerializer. After some digging, I found XmlSerializer chokes on 0x03 chars. It probably chokes on many others, but this one I found. It serializes ok, but chokes on...
3
3294
by: kimtherkelsen | last post by:
Hi, I want to send XML data from a server to some clients over a network connection using the TCP/IP protocol. If I send the XMLs as byte arrays I need to insert header information in the data to distinguish the XMLs from each other in the stream of data. Is there any way to avoid this (for instance by sending SOAP telegrams))? I have...
0
7912
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8202
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. ...
0
8216
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...
0
6614
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...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3837
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2345
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
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.