将xml元素属性序列化为子对象的属性

时间:2010-07-16 13:08:24

标签: c# xml-serialization

我遵循xml元素:

<point X="-1368.087158" Y="-918.482910" Z="156.191040" nX="0.241530" nY="-0.945819" nZ="0.217001"/>

以及以下对象结构:

public class Point
{
    [XmlAttribute("X")]
    public float X { get; set; }
    [XmlAttribute("Y")]
    public float Y { get; set; }
    [XmlAttribute("Z")]
    public float Z { get; set; }
}


public class Vertex: Point
{
    [Xml...]
    public Point Normal { get; set; }
}

如何序列化nX / nY / nZ?

1 个答案:

答案 0 :(得分:0)

在过去遇到类似这样的东西时,我会添加仅用于序列化的额外属性。因此,在您的情况下,您的Vertex类可能如下所示:

public class Vertex : Point
{
    [XmlIgnore]
    public Point Normal { get; set; }

    [XmlAttribute]
    public float nX
    {
        get { return Normal.X; }
        set { Normal.X = value; }
    }

    //etc
}