473,387 Members | 1,624 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,387 software developers and data experts.

Problem Serializing A Class to XML

I am trying to create an class (in vb.net) that will serialize to produce
xml as follows:
------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<TotalCostData>
<Ship DataType="Money" Currency="USD">1.00</Ship>
<Total DataType="Money" Currency="USD">35.00</Total>
</TotalCostData>

------------------------------------------
So I created the following class:
------------------------------------------
Imports System.Xml.Serialization

<XmlRoot("TotalCostData")> _
Public Class totalCost
Public Ship As New lcMoney
Public Total As New lcMoney
End Class

Public Class lcMoney
<XmlAttributeAttribute("DataType")> _
Public DataType As String = "Money"

<XmlAttributeAttribute("Currency")> _
Public Currency As String = "USD"

<XmlText()> _
Public Value As String
End Class

------------------------------------------
When I run the following vb.net code:
------------------------------------------
Dim tc As New totalCost
tc.Ship.Value = "1.00"
tc.Total.Value = "35.00"

Dim xmlForClass As String = SerializeObject(tc)
XmlToFile(outputFilepath1, xmlForClass)
------------------------------------------
I get the following output:
------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<TotalCostData xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Ship DataType="Money" Currency="USD">1.00</Ship>
<Total DataType="Money" Currency="USD">35.00</Total>
</TotalCostData>
------------------------------------------
Which is pretty much what I wanted and seems to work.

However, sometimes I don't set a value for Ship and I don't want it to
appear in the xml.
When I run the following code (without Ship):
------------------------------------------
Dim tc As New totalCost
tc.Total.Value = "35.00"

Dim xmlForClass As String = SerializeObject(tc)
XmlToFile(outputFilepath1, xmlForClass)
------------------------------------------
I get the following output:
------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<TotalCostData xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Ship DataType="Money" Currency="USD" />
<Total DataType="Money" Currency="USD">35.00</Total>
</TotalCostData>
------------------------------------------
But what I really want is:
------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<TotalCostData xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Total DataType="Money" Currency="USD">35.00</Total>
</TotalCostData>
------------------------------------------

How can I get rid of the Ship entry when I don't enter a value for it?

Thanks,
Dave
Nov 12 '05 #1
1 1885
In the case where there is no Ship value, you'll have to use logic to return
null for the Ship value. So:

tc.Ship.Value = Nothing
rizla

"Dave56" <no****@co.lane.or.us> wrote in message
news:u1*****************@TK2MSFTNGP11.phx.gbl...
I am trying to create an class (in vb.net) that will serialize to produce
xml as follows:
------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<TotalCostData>
<Ship DataType="Money" Currency="USD">1.00</Ship>
<Total DataType="Money" Currency="USD">35.00</Total>
</TotalCostData>

------------------------------------------
So I created the following class:
------------------------------------------
Imports System.Xml.Serialization

<XmlRoot("TotalCostData")> _
Public Class totalCost
Public Ship As New lcMoney
Public Total As New lcMoney
End Class

Public Class lcMoney
<XmlAttributeAttribute("DataType")> _
Public DataType As String = "Money"

<XmlAttributeAttribute("Currency")> _
Public Currency As String = "USD"

<XmlText()> _
Public Value As String
End Class

------------------------------------------
When I run the following vb.net code:
------------------------------------------
Dim tc As New totalCost
tc.Ship.Value = "1.00"
tc.Total.Value = "35.00"

Dim xmlForClass As String = SerializeObject(tc)
XmlToFile(outputFilepath1, xmlForClass)
------------------------------------------
I get the following output:
------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<TotalCostData xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Ship DataType="Money" Currency="USD">1.00</Ship>
<Total DataType="Money" Currency="USD">35.00</Total>
</TotalCostData>
------------------------------------------
Which is pretty much what I wanted and seems to work.

However, sometimes I don't set a value for Ship and I don't want it to
appear in the xml.
When I run the following code (without Ship):
------------------------------------------
Dim tc As New totalCost
tc.Total.Value = "35.00"

Dim xmlForClass As String = SerializeObject(tc)
XmlToFile(outputFilepath1, xmlForClass)
------------------------------------------
I get the following output:
------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<TotalCostData xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Ship DataType="Money" Currency="USD" />
<Total DataType="Money" Currency="USD">35.00</Total>
</TotalCostData>
------------------------------------------
But what I really want is:
------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<TotalCostData xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Total DataType="Money" Currency="USD">35.00</Total>
</TotalCostData>
------------------------------------------

How can I get rid of the Ship entry when I don't enter a value for it?

Thanks,
Dave

Nov 12 '05 #2

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

Similar topics

4
by: Angelos Karantzalis | last post by:
Hi guys. I've come across a problem when I tried to serialize a class into xml, only to discover that the parent class's XML Serialization properties weren't included in the output xml. ...
0
by: okinrus | last post by:
Can someone take a look at this code and figure out why Serializable_base::add_serializer throws std::bad_alloc. The problem seems to be the compiler because msvc++ 7.1 says...
1
by: Thomas | last post by:
Hi, I implemented a composite pattern which should be serializable to xml. After spending some time in the newsgroups, i finally managed serializing, even with utf-8 instead of utf-16, which...
1
by: dotnetnewbie | last post by:
Hi all I am new to .NET and webservice so I wish someone can shed some light on me. I have a Project class and a Product class, the Project can contain multiple Products (as an ArrayList). In...
2
by: Tobias Zimmergren | last post by:
Hi, just wondering what serializing really is, and howto use it? Thanks. Tobias __________________________________________________________________ Tobias ICQ#: 55986339 Current ICQ status: +...
1
by: Derrick | last post by:
Hello all; I seem to be having some trouble serializing a class to XML. This code is a cut & paste from a project which used it perfectly, but all of a sudden I'm getting an error that the "dll...
7
by: Joe | last post by:
I've tracked the performance issue down to a single class. This class derives from CollectionBase and stores a basic value type such as string, int, double, etc... I also store the type itself...
15
by: Cesar Ronchese | last post by:
Hi, I built the sample code showing the problem with dates when viewed at different machines, different Time Zones and transported via Remoting. The zip can be downloaded here: ...
9
by: norvinl | last post by:
Hi, I'm serializing a class and using shared memory / deserialization to send it to other processes. I can serialize with one app and deserialize with another instance of the same app. But...
5
by: Nirdesh | last post by:
Hi, I am serializing a custom class holding some data for my project. This data internally contains a class which contains an event public delegate void MemberModifiedEventHandler(object...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
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
0
BarryA
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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,...
0
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...

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.