我正在使用带有矩形的 xml 序列化,但这会产生一些讨厌的 XML ...
我的课是这样的:
    [Serializable]
    public class myObject
    {
       public Rectangle Region { get; set; }
       //Some other properties and methods...
    }
当我将它序列化为 XML 时,这给了我这个:
    <myObject>
      <Region>
        <Location>
          <X>141</X>
          <Y>93</Y>
        </Location>
        <Size>
          <Width>137</Width>
          <Height>15</Height>
        </Size>
        <X>141</X>
        <Y>93</Y>
        <Width>137</Width>
        <Height>15</Height>
      </Region>
      ...
    </myObject>
呸!
我希望我可以抑制Size和Location上的属性Rectangle,或者使用支持变量并[XmlIgnore]最终得到如下结果:
    [Serializable]
    public class myObject
    {
       [XmlElement("????")]
       public int RegionX;
       [XmlElement("????")]
       public int RegionY;
       [XmlElement("????")]
       public int RegionHeight;
       [XmlElement("????")]
       public int RegionWidth;
       [XmlIgnore]
       public Rectangle Region {get { return new Rectangle(RegionX, RegionY, RegionWidth, RegionHeight);}
       //Some other properties and methods...
    }
希望能给我类似的东西:
    <myObject>
      <Region>
        <X>141</X>
        <Y>93</Y>
        <Width>137</Width>
        <Height>15</Height>
      </Region>
      ...
    </myObject>
代码不太好,但是 XML 将由人们编辑,所以最好能得到一些在那里工作的东西......
任何想法可能会出现在“????”中?或另一种方法?
我宁愿不必实现我自己的版本Rectangle...