C# 不使用新对象将XML序列化到子节点
我正在使用xml序列化,使用矩形,但这会产生一些令人讨厌的xml 我的班级是这样的:C# 不使用新对象将XML序列化到子节点,c#,.net,xml-serialization,C#,.net,Xml Serialization,我正在使用xml序列化,使用矩形,但这会产生一些令人讨厌的xml 我的班级是这样的: [Serializable] public class myObject { public Rectangle Region { get; set; } //Some other properties and methods... } [Serializable] public class myObject {
[Serializable]
public class myObject
{
public Rectangle Region { get; set; }
//Some other properties and methods...
}
[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...
}
[XmlElement("Region",typeof(XmlRectangle))]
public Rectangle Region { get; set; }
当我将其序列化为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>
希望能给我一些类似的东西:
<myObject>
<Region>
<X>141</X>
<Y>93</Y>
<Width>137</Width>
<Height>15</Height>
</Region>
...
</myObject>
141
93
137
15
...
代码不是很好,但是XML将由人们编辑,因此获得一些在那里工作的东西会很好
有什么可能进入“?”的IDE吗?还是另一种方法
我不想实现我自己版本的
Rectangle
..谢谢大家的评论,我最终走了一条DTO路线——我实现了我自己的Struct-XmlRectangle
,保存了我需要用[Serializable]
修饰的四个int值。我添加了隐式转换运算符,以便将其用作矩形
:
[Serializable]
public struct XmlRectangle
{
#endregion Public Properties
public int X {get; set; }
public int Y {get; set; }
public int Height { get; set; }
public int Width { get; set; }
#endregion Public Properties
#region Implicit Conversion Operators
public static implicit operator Rectangle(XmlRectangle xmlRectangle)
{
return new Rectangle(xmlRectangle.X, xmlRectangle.Y, xmlRectangle.Width, xmlRectangle.Height);
}
public static implicit operator XmlRectangle(Rectangle rectangle)
{
return result = new XmlRectangle(){ X = rectangle.X, Y = Rectangle.Y, Height = Rectangle.Height, width = Rectangle.Width };
}
#endregion Implicit Conversion Operators
}
然后,将其作为数据保存的类有一个Rectangle
属性,将其作为XmlRectangle
公开给序列化程序,如下所示:
[Serializable]
public class myObject
{
public Rectangle Region { get; set; }
//Some other properties and methods...
}
[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...
}
[XmlElement("Region",typeof(XmlRectangle))]
public Rectangle Region { get; set; }
如果将数据序列化层与业务对象分离,则可以执行任何您喜欢的操作。创建您自己的
矩形到
对象,该对象具有属性,例如X
,Y
,宽度
,高度
,然后通过转换器运行DTO对象,将其更改为业务对象或从业务对象更改为业务对象;如果域对象不是序列化所需的形状,那么不要与序列化程序抗争-而是添加DTO层。为什么人们要手动编辑XML?编辑们正在为此而努力。无论如何,您可以简单地创建属性来公开矩形
字段,并序列化它们,而不是字段本身。如果实现ISerializable接口,您几乎可以做任何事情。但是,我不确定XMLSerializer是否使用了该选项。