Sign in to follow this  

[.net] Serialize a XNA Vector3 type

This topic is 3492 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

How can I serialize a XNA Vector3 type into xml attributes? Only way I can think of is to write a new Vector3 class and implement the attribute tags on it. something like this:

<scene>
    <scenemesh name="test" x="5" y="5" z="5" />
</scene>


C#

[Serializable]
public class SceneMesh
{
    [XmlAttribute("name", typeof(string))]
    public string name;

    [XmlAttribute(typeof(Vector3))]
    public Vector3 position;
}


Share this post


Link to post
Share on other sites
The only thing I can think of as a quick solution is exactly what you said - Make another Vector3 class that is actually serializable. Or as a quick fix you could just add 3 double properties to that scene mesh class called X, Y and Z and then each one would return your_vector3.X / Y or Z. Then just add the attribbutes to those properties and not the Vector3 itself. Like this:


[Serializable]
public class SceneMesh
{
private Vector3 _pos;

[XmlAttribute("name", typeof(string))]
public string name{ set; get; }
public Vector3 position
{
set{_pos = value;}
get{return _pos; }
}

[XmlAttribute("x", typeof(double))]
public double posX
{
get{ return _pos.X; }
}

// ... etc etc

}



Hope it helps,

James

Share this post


Link to post
Share on other sites

This topic is 3492 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.

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