Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SerializableClasses : how to seriailize properties, not class name. 1

Status
Not open for further replies.

gdrenfrew

Programmer
Aug 1, 2002
227
GB
Hello,

I've got a class I want to serialize into XML.

Simple example:
The class is called "ElementClass" and contains the property "ElementName" which I give the value of "Commodity".

Can I serialize it so the XML output is along the lines of
"<Commodity>", and not "<ElementClass>"?

Or does serializing a class always create an XML element (or attribute) called whatever the class is called?

Thanks
 
When you serialize the contents of a class, when it comes time to de-serialize it, you want the serialized data to be able to go back into an instance of the same class, right? So the root xml element then uses the name of the class, and not some arbitrary name.

Another way of thinking about it is that "Commodity" is a property of the class, and not the class itself. If you have a Ballon class (ballon.cs), it wouldn't make sense for it to serialize to a bit of xml called "red", right? Because if that were true, having the xml called "true" (from the IsInflated property) would also be a valid name.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks for the reply Chip.

Good explanation, that makes sense. My way of wanting to do things does look a bit hare-brained when you explain it like the Balloon example.

I've ended up just using XmlTextWriter and not bothering building the data into classes (I don't need to deserialize it).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top