开发者

Serialize xmls element attributes to properties of child object

开发者 https://www.devze.com 2023-01-07 09:42 出处:网络
I\'ve following xml element: <point X=\"-1368.087158\" Y=\"-918.482910\" Z=\"156.191040\" nX=\"0.241530\" nY=\"-0.945819\" nZ=\"0.217001\"/>

I've following xml element:

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

and following object structure:

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; }
}

How can I serialize nX/nY/nZ?


In the past when hit with something like this I would add extra properties that only are to be used for serialization. So in your case, your Vertex class might look like this:

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

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

    //etc
}
0

精彩评论

暂无评论...
验证码 换一张
取 消