代码之家  ›  专栏  ›  技术社区  ›  David

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

  •  0
  • David  · 技术社区  · 16 年前

    我有以下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 回复  |  直到 16 年前
        1
  •  0
  •   Jake Pearson    16 年前

    在过去,当遇到类似这样的问题时,我会添加一些只用于序列化的额外属性。因此,在您的例子中,顶点类可能如下所示:

    public class Vertex : Point
    {
        [XmlIgnore]
        public Point Normal { get; set; }
    
        [XmlAttribute]
        public float nX
        {
            get { return Normal.X; }
            set { Normal.X = value; }
        }
    
        //etc
    }