469,950 Members | 1,924 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

XML Serializer - Skip creation of Blank/Empty Class members

I have a class I am serializing, and need the resultant XML to skip/omit
classes that are not initialized, or their member variables have not been
set. Is this possible?

Say for the following code, sometimes the Person may not have given us one,
both, or either of their addresses.

The serialize function will still put in a "<BillingAddress />" tag
indicating no data. I want it to completely omit the tag if the class is
empty, similar to the way it omits any tag for blank STRING members, such as
if there is no SSN given.

VB.NET SAMPLE CODE:
==========================
Public Class clsPerson
Public FirstName As String
Public LastName As String
Public BillingAddress As New clsAddress()
Public ShippingAddress As New clsAddress()
Public SSN As String
End Class

Public Class clsAddress
Public AddressLine1 As String
Public AddressLine2 As String
Public City As String
Public StateCode As String
Public ZipCode As String
End Class

Public Function BuildXML()
Dim oPerson As New clsPerson()
oPerson.FirstName = "John"
oPerson.LastName = "Smith"
'NOTE THERE IS NO ADDRESS INFO or SSN INFO ASSIGNED.

Dim xmlS1 As New XmlSerializer(GetType(clsPerson))
Dim swri1 As New StringWriter()
Dim xwri1 As New XmlTextWriter(swri1)

xwri1.Formatting = Formatting.Indented
xmlS1.Serialize(swri1, oPerson)

'Copy to Clipboard for EZ Access While Troubleshooting
Clipboard.SetDataObject(swri1.ToString())
MsgBox(swri1.ToString())
swri1.Close()
End Function
RESULT
==========

<?xml version="1.0" encoding="utf-16"?>
<clsPerson xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<FirstName>John</FirstName>
<LastName>Smith</LastName>
<BillingAddress />
<ShippingAddress />
</clsPerson>

'NOTICE THERE IS A TAG FOR EACH CLASS EVEN THOUGH EMPTY, BUT NO TAG FOR SSN,
THOUGH IT WAS ALSO EMPTY/BLANK.

For this example, it is really no big deal if I were to just include them.
But, in my code I am trying to do, I literally have many nested classes with
many nested classes with many nested classes and I am really generating a
lot of blank tags.

Any ideas? Thanks!
Nov 11 '05 #1
5 2127
You are initializing the fields BillingAddress and ShippingAddress to empty
objects. Therefore the serializer does write the root element for these
objects. You need to either a) only initialize them an instance of
clsAddress when you have data, or b) set them explicitly to Nothing when you
have no data.

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"Tappy Tibbons" <ta**********@sbcglobal.net> wrote in message
news:#P**************@TK2MSFTNGP11.phx.gbl...
I have a class I am serializing, and need the resultant XML to skip/omit
classes that are not initialized, or their member variables have not been
set. Is this possible?

Say for the following code, sometimes the Person may not have given us one, both, or either of their addresses.

The serialize function will still put in a "<BillingAddress />" tag
indicating no data. I want it to completely omit the tag if the class is
empty, similar to the way it omits any tag for blank STRING members, such as if there is no SSN given.

VB.NET SAMPLE CODE:
==========================
Public Class clsPerson
Public FirstName As String
Public LastName As String
Public BillingAddress As New clsAddress()
Public ShippingAddress As New clsAddress()
Public SSN As String
End Class

Public Class clsAddress
Public AddressLine1 As String
Public AddressLine2 As String
Public City As String
Public StateCode As String
Public ZipCode As String
End Class

Public Function BuildXML()
Dim oPerson As New clsPerson()
oPerson.FirstName = "John"
oPerson.LastName = "Smith"
'NOTE THERE IS NO ADDRESS INFO or SSN INFO ASSIGNED.

Dim xmlS1 As New XmlSerializer(GetType(clsPerson))
Dim swri1 As New StringWriter()
Dim xwri1 As New XmlTextWriter(swri1)

xwri1.Formatting = Formatting.Indented
xmlS1.Serialize(swri1, oPerson)

'Copy to Clipboard for EZ Access While Troubleshooting
Clipboard.SetDataObject(swri1.ToString())
MsgBox(swri1.ToString())
swri1.Close()
End Function
RESULT
==========

<?xml version="1.0" encoding="utf-16"?>
<clsPerson xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<FirstName>John</FirstName>
<LastName>Smith</LastName>
<BillingAddress />
<ShippingAddress />
</clsPerson>

'NOTICE THERE IS A TAG FOR EACH CLASS EVEN THOUGH EMPTY, BUT NO TAG FOR SSN, THOUGH IT WAS ALSO EMPTY/BLANK.

For this example, it is really no big deal if I were to just include them.
But, in my code I am trying to do, I literally have many nested classes with many nested classes with many nested classes and I am really generating a
lot of blank tags.

Any ideas? Thanks!

Nov 11 '05 #2
That's what I was afraid of. Do you know of a slick way to do either?

The only thing I could think of was to manually set my classes to NEW
instances, either manually via a special function call before any child
members are set, or make some sort of a wrapper function to do it
automatically before one it's children variables is set. Both seem kind of
kludgy.

I also thought about leaving it as it is, and at the end looping through,
and somehow testing if any of the children members had been assigned
anything, and if not, set it to NOTHING, but I can't come up with a good way
of iterating through and testing the child member vars.

Thanks for any help you can give....
"Tappy Tibbons" <ta**********@sbcglobal.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
I have a class I am serializing, and need the resultant XML to skip/omit
classes that are not initialized, or their member variables have not been
set. Is this possible?

Say for the following code, sometimes the Person may not have given us one, both, or either of their addresses.

The serialize function will still put in a "<BillingAddress />" tag
indicating no data. I want it to completely omit the tag if the class is
empty, similar to the way it omits any tag for blank STRING members, such as if there is no SSN given.

VB.NET SAMPLE CODE:
==========================
Public Class clsPerson
Public FirstName As String
Public LastName As String
Public BillingAddress As New clsAddress()
Public ShippingAddress As New clsAddress()
Public SSN As String
End Class

Public Class clsAddress
Public AddressLine1 As String
Public AddressLine2 As String
Public City As String
Public StateCode As String
Public ZipCode As String
End Class

Public Function BuildXML()
Dim oPerson As New clsPerson()
oPerson.FirstName = "John"
oPerson.LastName = "Smith"
'NOTE THERE IS NO ADDRESS INFO or SSN INFO ASSIGNED.

Dim xmlS1 As New XmlSerializer(GetType(clsPerson))
Dim swri1 As New StringWriter()
Dim xwri1 As New XmlTextWriter(swri1)

xwri1.Formatting = Formatting.Indented
xmlS1.Serialize(swri1, oPerson)

'Copy to Clipboard for EZ Access While Troubleshooting
Clipboard.SetDataObject(swri1.ToString())
MsgBox(swri1.ToString())
swri1.Close()
End Function
RESULT
==========

<?xml version="1.0" encoding="utf-16"?>
<clsPerson xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<FirstName>John</FirstName>
<LastName>Smith</LastName>
<BillingAddress />
<ShippingAddress />
</clsPerson>

'NOTICE THERE IS A TAG FOR EACH CLASS EVEN THOUGH EMPTY, BUT NO TAG FOR SSN, THOUGH IT WAS ALSO EMPTY/BLANK.

For this example, it is really no big deal if I were to just include them.
But, in my code I am trying to do, I literally have many nested classes with many nested classes with many nested classes and I am really generating a
lot of blank tags.

Any ideas? Thanks!

Nov 11 '05 #3
How do you actually populate your Person objects?

Can you just change the class definition to:

Public Class clsPerson
Public FirstName As String
Public LastName As String
Public BillingAddress As clsAddress()
Public ShippingAddress As clsAddress()
Public SSN As String
End Class

and assign BillingAddress and ShippingAddress when the data was entered?
There is also another option but it also requires knowledge if data is
present or not, so I'd stick with a method that instantiates the clsAddress
object adds it to Person and sets the properties. That's just the way the
XmlSerializer works ...

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"Tappy Tibbons" <ta**********@sbcglobal.net> wrote in message
news:em**************@tk2msftngp13.phx.gbl...
That's what I was afraid of. Do you know of a slick way to do either?

The only thing I could think of was to manually set my classes to NEW
instances, either manually via a special function call before any child
members are set, or make some sort of a wrapper function to do it
automatically before one it's children variables is set. Both seem kind of
kludgy.

I also thought about leaving it as it is, and at the end looping through,
and somehow testing if any of the children members had been assigned
anything, and if not, set it to NOTHING, but I can't come up with a good way of iterating through and testing the child member vars.

Thanks for any help you can give....
"Tappy Tibbons" <ta**********@sbcglobal.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
I have a class I am serializing, and need the resultant XML to skip/omit
classes that are not initialized, or their member variables have not been set. Is this possible?

Say for the following code, sometimes the Person may not have given us one,
both, or either of their addresses.

The serialize function will still put in a "<BillingAddress />" tag
indicating no data. I want it to completely omit the tag if the class is
empty, similar to the way it omits any tag for blank STRING members, such as
if there is no SSN given.

VB.NET SAMPLE CODE:
==========================
Public Class clsPerson
Public FirstName As String
Public LastName As String
Public BillingAddress As New clsAddress()
Public ShippingAddress As New clsAddress()
Public SSN As String
End Class

Public Class clsAddress
Public AddressLine1 As String
Public AddressLine2 As String
Public City As String
Public StateCode As String
Public ZipCode As String
End Class

Public Function BuildXML()
Dim oPerson As New clsPerson()
oPerson.FirstName = "John"
oPerson.LastName = "Smith"
'NOTE THERE IS NO ADDRESS INFO or SSN INFO ASSIGNED.

Dim xmlS1 As New XmlSerializer(GetType(clsPerson))
Dim swri1 As New StringWriter()
Dim xwri1 As New XmlTextWriter(swri1)

xwri1.Formatting = Formatting.Indented
xmlS1.Serialize(swri1, oPerson)

'Copy to Clipboard for EZ Access While Troubleshooting
Clipboard.SetDataObject(swri1.ToString())
MsgBox(swri1.ToString())
swri1.Close()
End Function
RESULT
==========

<?xml version="1.0" encoding="utf-16"?>
<clsPerson xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<FirstName>John</FirstName>
<LastName>Smith</LastName>
<BillingAddress />
<ShippingAddress />
</clsPerson>

'NOTICE THERE IS A TAG FOR EACH CLASS EVEN THOUGH EMPTY, BUT NO TAG FOR

SSN,
THOUGH IT WAS ALSO EMPTY/BLANK.

For this example, it is really no big deal if I were to just include

them. But, in my code I am trying to do, I literally have many nested classes

with
many nested classes with many nested classes and I am really generating a lot of blank tags.

Any ideas? Thanks!


Nov 11 '05 #4
Something like this? Can you think of anything more elegant?
=========================

Public Class clsPerson
Public FirstName As String
Public LastName As String
Public BillingAddress As clsAddress
Public ShippingAddress As clsAddress
Public SSN As String

Public Sub BillingAddress_New()
BillingAddress = New clsAddress()
End Sub

Public Sub ShippingAddress_New()
ShippingAddress = New clsAddress()
End Sub

End Class

USAGE:
==========================
Dim oPerson As New clsPerson()
oPerson.FirstName = "John"
oPerson.LastName = "Smith"
oPerson.BillingAddress_New()
oPerson.BillingAddress.AddressLine1 = "12345 6th Street"
oPerson.BillingAddress.City = "Fairfield"


"Christoph Schittko [MVP]" <ch********************@austin.rr.com> wrote in
message news:eJ**************@TK2MSFTNGP11.phx.gbl...
How do you actually populate your Person objects?

Can you just change the class definition to:

Public Class clsPerson
Public FirstName As String
Public LastName As String
Public BillingAddress As clsAddress()
Public ShippingAddress As clsAddress()
Public SSN As String
End Class

and assign BillingAddress and ShippingAddress when the data was entered?
There is also another option but it also requires knowledge if data is
present or not, so I'd stick with a method that instantiates the clsAddress object adds it to Person and sets the properties. That's just the way the
XmlSerializer works ...

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"Tappy Tibbons" <ta**********@sbcglobal.net> wrote in message
news:em**************@tk2msftngp13.phx.gbl...
That's what I was afraid of. Do you know of a slick way to do either?

The only thing I could think of was to manually set my classes to NEW
instances, either manually via a special function call before any child
members are set, or make some sort of a wrapper function to do it
automatically before one it's children variables is set. Both seem kind of
kludgy.

I also thought about leaving it as it is, and at the end looping through, and somehow testing if any of the children members had been assigned
anything, and if not, set it to NOTHING, but I can't come up with a good way
of iterating through and testing the child member vars.

Thanks for any help you can give....
"Tappy Tibbons" <ta**********@sbcglobal.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
I have a class I am serializing, and need the resultant XML to skip/omit classes that are not initialized, or their member variables have not been set. Is this possible?

Say for the following code, sometimes the Person may not have given us

one,
both, or either of their addresses.

The serialize function will still put in a "<BillingAddress />" tag
indicating no data. I want it to completely omit the tag if the class is empty, similar to the way it omits any tag for blank STRING members, such
as
if there is no SSN given.

VB.NET SAMPLE CODE:
==========================
Public Class clsPerson
Public FirstName As String
Public LastName As String
Public BillingAddress As New clsAddress()
Public ShippingAddress As New clsAddress()
Public SSN As String
End Class

Public Class clsAddress
Public AddressLine1 As String
Public AddressLine2 As String
Public City As String
Public StateCode As String
Public ZipCode As String
End Class

Public Function BuildXML()
Dim oPerson As New clsPerson()
oPerson.FirstName = "John"
oPerson.LastName = "Smith"
'NOTE THERE IS NO ADDRESS INFO or SSN INFO ASSIGNED.

Dim xmlS1 As New XmlSerializer(GetType(clsPerson))
Dim swri1 As New StringWriter()
Dim xwri1 As New XmlTextWriter(swri1)

xwri1.Formatting = Formatting.Indented
xmlS1.Serialize(swri1, oPerson)

'Copy to Clipboard for EZ Access While Troubleshooting
Clipboard.SetDataObject(swri1.ToString())
MsgBox(swri1.ToString())
swri1.Close()
End Function
RESULT
==========

<?xml version="1.0" encoding="utf-16"?>
<clsPerson xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<FirstName>John</FirstName>
<LastName>Smith</LastName>
<BillingAddress />
<ShippingAddress />
</clsPerson>

'NOTICE THERE IS A TAG FOR EACH CLASS EVEN THOUGH EMPTY, BUT NO TAG

FOR SSN,
THOUGH IT WAS ALSO EMPTY/BLANK.

For this example, it is really no big deal if I were to just include them. But, in my code I am trying to do, I literally have many nested
classes with
many nested classes with many nested classes and I am really

generating a lot of blank tags.

Any ideas? Thanks!



Nov 11 '05 #5
How about:

Public Class clsAddress
Public Sub New( line1 As String, line2 As String, ci As String, state
As String, zip As String )
AddressLine1 = line1
AddressLine2 = line2
City = ci
StateCode = state
ZipCode = zip
End Sub

Public Sub New()
' Make the XmlSerializer happy
End Sub

Public AddressLine1 As String
Public AddressLine2 As String
Public City As String
Public StateCode As String
Public ZipCode As String
End Class

Public Sub AddBillingAddress( line1 As String, line2 As String, ci As
String, state As String, zip As String )
BillingAddress = New clsAddress( line1, line2, ci, state, zip )
End Sub

Public Sub AddShippingAddress( line1 As String, line2 As String, ci As
String, state As String, zip As String )
ShippingAddress = New clsAddress( line1, line2, ci, state, zip )
End Sub
--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor
"Tappy Tibbons" <ta**********@sbcglobal.net> wrote in message
news:ub**************@TK2MSFTNGP10.phx.gbl...
Something like this? Can you think of anything more elegant?
=========================

Public Class clsPerson
Public FirstName As String
Public LastName As String
Public BillingAddress As clsAddress
Public ShippingAddress As clsAddress
Public SSN As String

Public Sub BillingAddress_New()
BillingAddress = New clsAddress()
End Sub

Public Sub ShippingAddress_New()
ShippingAddress = New clsAddress()
End Sub

End Class

USAGE:
==========================
Dim oPerson As New clsPerson()
oPerson.FirstName = "John"
oPerson.LastName = "Smith"
oPerson.BillingAddress_New()
oPerson.BillingAddress.AddressLine1 = "12345 6th Street"
oPerson.BillingAddress.City = "Fairfield"


"Christoph Schittko [MVP]" <ch********************@austin.rr.com> wrote in
message news:eJ**************@TK2MSFTNGP11.phx.gbl...
How do you actually populate your Person objects?

Can you just change the class definition to:

Public Class clsPerson
Public FirstName As String
Public LastName As String
Public BillingAddress As clsAddress()
Public ShippingAddress As clsAddress()
Public SSN As String
End Class

and assign BillingAddress and ShippingAddress when the data was entered?
There is also another option but it also requires knowledge if data is
present or not, so I'd stick with a method that instantiates the clsAddress
object adds it to Person and sets the properties. That's just the way the
XmlSerializer works ...

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"Tappy Tibbons" <ta**********@sbcglobal.net> wrote in message
news:em**************@tk2msftngp13.phx.gbl...
That's what I was afraid of. Do you know of a slick way to do either?

The only thing I could think of was to manually set my classes to NEW
instances, either manually via a special function call before any child members are set, or make some sort of a wrapper function to do it
automatically before one it's children variables is set. Both seem kind of kludgy.

I also thought about leaving it as it is, and at the end looping through, and somehow testing if any of the children members had been assigned
anything, and if not, set it to NOTHING, but I can't come up with a
good
way
of iterating through and testing the child member vars.

Thanks for any help you can give....
"Tappy Tibbons" <ta**********@sbcglobal.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
> I have a class I am serializing, and need the resultant XML to
skip/omit > classes that are not initialized, or their member variables have not

been
> set. Is this possible?
>
> Say for the following code, sometimes the Person may not have given
us one,
> both, or either of their addresses.
>
> The serialize function will still put in a "<BillingAddress />" tag
> indicating no data. I want it to completely omit the tag if the

class is > empty, similar to the way it omits any tag for blank STRING members,

such
as
> if there is no SSN given.
>
> VB.NET SAMPLE CODE:
> ==========================
> Public Class clsPerson
> Public FirstName As String
> Public LastName As String
> Public BillingAddress As New clsAddress()
> Public ShippingAddress As New clsAddress()
> Public SSN As String
> End Class
>
> Public Class clsAddress
> Public AddressLine1 As String
> Public AddressLine2 As String
> Public City As String
> Public StateCode As String
> Public ZipCode As String
> End Class
>
> Public Function BuildXML()
> Dim oPerson As New clsPerson()
> oPerson.FirstName = "John"
> oPerson.LastName = "Smith"
> 'NOTE THERE IS NO ADDRESS INFO or SSN INFO ASSIGNED.
>
> Dim xmlS1 As New XmlSerializer(GetType(clsPerson))
> Dim swri1 As New StringWriter()
> Dim xwri1 As New XmlTextWriter(swri1)
>
> xwri1.Formatting = Formatting.Indented
> xmlS1.Serialize(swri1, oPerson)
>
> 'Copy to Clipboard for EZ Access While Troubleshooting
> Clipboard.SetDataObject(swri1.ToString())
> MsgBox(swri1.ToString())
> swri1.Close()
> End Function
>
>
> RESULT
> ==========
>
> <?xml version="1.0" encoding="utf-16"?>
> <clsPerson xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
> <FirstName>John</FirstName>
> <LastName>Smith</LastName>
> <BillingAddress />
> <ShippingAddress />
> </clsPerson>
>
> 'NOTICE THERE IS A TAG FOR EACH CLASS EVEN THOUGH EMPTY, BUT NO TAG FOR SSN,
> THOUGH IT WAS ALSO EMPTY/BLANK.
>
> For this example, it is really no big deal if I were to just include

them.
> But, in my code I am trying to do, I literally have many nested classes with
> many nested classes with many nested classes and I am really

generating
a
> lot of blank tags.
>
> Any ideas? Thanks!
>
>



Nov 11 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Emmanuel Thomé | last post: by
4 posts views Thread by William Payne | last post: by
3 posts views Thread by puzzlecracker | last post: by
3 posts views Thread by Mark | last post: by
6 posts views Thread by Wilfried Mestdagh | last post: by
8 posts views Thread by Q. John Chen | last post: by
10 posts views Thread by John | last post: by
code green
2 posts views Thread by code green | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.