I need to construct an XML transaction of the following format:
<RateV3Request USERID="MyId">
<Package ID="1ST">
<Service>ALL</Service>
<FirstClassMailType>LETTER</FirstClassMailType>
<ZipOrigination>06226</ZipOrigination>
<ZipDestination>06231</ZipDestination>
<Pounds>0</Pounds>
<Ounces>4.5</Ounces>
<Size>REGULAR</Size>
<Machinable>true</Machinable>
<ShipDate Option="EMSH">21-Dec-2009</ShipDate>
</Package>
</RateV3Request>
My thought was to create an object RateV3Request that contains typed properties for each XML property (I assume property is not the correct word here.)
I was thinking of creating a “GetXml” function on my object that returns the XML representation of the values in the object instance.I could do all the string concatenation myself, but surely there is a cleaner way.
Suggestions? I am using VB.NET
The XmlSerializer should do this work for you. You can use XML attributes to control how your properties and fields should be serialized to XML (e.g. as an attribute, as a specific element, not at all, …).
For example, your
Packageclass could look like this (untested):Then you can do something like this (untested):