Sign in to follow this  

[.net] Deserialize XML Text Element to Custom Object

Recommended Posts

The XML serialization/deserialization framework included in .NET will automatically deserialize the text contents of an element into most fundamental types (bool, int, string, etc.), but I'm wondering if there's a way to extend that so that I could turn an element like this:

<Vector>0, 0, 0</Vector>

into a custom Vector object, for example. I've read through a lot of the documentation and found how to deserialize complex objects with child elements, but I haven't seen a way to parse the inner text differently as I would need to for the above. The only way I know to do that currently is deserialize it as a string and manually parse it after the fact for each Vector element, or to do this:


I know I need to write the code to split it up and parse the values, I'm just wondering if I can set it up so that it automatically happens if a Vector object is being deserialized.

Anyone know about this? Thanks!

Share this post

Link to post
Share on other sites
You can use a read/write property to do this: [url=""]http://codethatworks...zed-attributes/[/url]

Make a string property, have its 'get' portion construct the comma-separated-string from the X,Y,Z fields. Then make the 'set' property take an input string and parse it out into the X,Y,Z fields. Then add the XmlIgnore attribute to the X,Y,Z fields so that they get skipped individually during (de)serialization.

The (de)serializer will operate on the property, which will automatically compose/decompose your X,Y,Z parts.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this