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