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! 5 2289
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!
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!
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!
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!
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! > >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Mark |
last post by:
Hi all,
i'm trying to serialize a class. Using the constructor of
XmlSerializer i get these (odd?) errors: "File or assembly name
goseij9w.dll, or one of its dependencies, was not found"....
|
by: Emmanuel Thomé |
last post by:
This is a comment aside the empty class behavior FAQ.
I understand there are a fair number of reasons which make empty
classes have non-zero size (except as base classes). If ``class foo''
were...
|
by: William Payne |
last post by:
Hello, I was under the impression that if I made a class Foo and if I didn't
specify a copy constructor I would get one anyway that simply assigns the
member variables (and that won't work for...
|
by: puzzlecracker |
last post by:
I want to read lines and skip blank lines:
would this work considering the lines can contain tabs, spaces, etc.?
file.in:
------
line1
line2
|
by: Mark |
last post by:
Hi all,
i'm trying to serialize a class. Using the constructor of
XmlSerializer i get these (odd?) errors: "File or assembly name
goseij9w.dll, or one of its dependencies, was not found"....
|
by: Wilfried Mestdagh |
last post by:
Hi,
I use this class to save application settings in a xml file. Sometime I have
exception error in the Load method, and sometime in the Save method. Is this
a bug in NET or is there something I...
|
by: Q. John Chen |
last post by:
I have following code:
public class Member
{
public string FirstName;
public string LastName;
public string GetXml()
{
StringBuilder sb = new StringBuilder();
XmlSerializer serializer
|
by: John |
last post by:
Hi,
How can I achieve that a childs constructor is only callable from it's parent?
Must I declare the class Private within A?
Thanks in Advance
Public MustInherit Class A
Protected Sub...
|
by: code green |
last post by:
I am trying to skip empty rows in a csv file
The manual states But none of these tests seem to work.
Anybody know why
while(($data = fgetcsv($this->handle)) !== false)
{
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
| |