Jump to content
  • Advertisement
Sign in to follow this  

[.net] Deserialize XML Text Element to Custom Object

This topic is 2770 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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: http://codethatworks...zed-attributes/

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!