我在c#:List<List<OVReady.Types.PointF[]>>
中有一个嵌套列表,需要序列化为xml
我的代码:
[XmlRoot("AlertInfo")]
public class AlertInfo
{
[XmlElement("TargetID")]
public string strTargetId { get; set; }
[XmlElement("ChannelID")]
public string strChId { get; set; }
[XmlElement("Timestamp")]
public string strTimestamp { get; set; }
[XmlElement("Object")]
public RectObject rfObject { get; set; }
[XmlArray("Polygons")]
[XmlArrayItem("Polygon")]
public List<List<OVReady.Types.PointF[]>> lstPolygons { get; set; }
}
public class RectObject
{
[XmlAttribute("x")]
public float x { get; set; }
[XmlAttribute("y")]
public float y { get; set; }
[XmlAttribute("width")]
public float width { get; set; }
[XmlAttribute("height")]
public float height { get; set; }
}
我得到了什么:
<AlertInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<TargetID>730</TargetID>
<ChannelID>613</ChannelID>
<Timestamp>2014-09-26 19:56:07:5660</Timestamp>
<Object x="0.24375" y="0.025" width="0.259375" height="0.9375001" />
<Polygons>
<Polygon>
<ArrayOfPointF>
<PointF>
<X xmlns="http://www.objectvideo.com/schemas/ovready">0.30625</X>
<Y xmlns="http://www.objectvideo.com/schemas/ovready">0.9375</Y>
</PointF>
<PointF>
<X xmlns="http://www.objectvideo.com/schemas/ovready">0.696875</X>
<Y xmlns="http://www.objectvideo.com/schemas/ovready">0.9416667</Y>
</PointF>
</ArrayOfPointF>
</Polygon>
</Polygons>
</AlertInfo>
我想要的是什么:
<AlertInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<TargetID>730</TargetID>
<ChannelID>613</ChannelID>
<Timestamp>2014-09-26 19:56:07:5660</Timestamp>
<Object x="0.24375" y="0.025" width="0.259375" height="0.9375001" />
<Polygons>
<Polygon>
<Point x=0.30625 y=0.9375>
<Point x=0.696875 y=0.9416667>
</Polygon>
</Polygons>
</AlertInfo>
如何删除标签并根据需要设置位置x和y?
答案 0 :(得分:2)
让我们从最简单的方法开始。重新设计您的财产以便更容易序列化。
为了能够实现这一目标,我必须引入一个新类Polygon:
public class Polygon
{
[XmlElement("Point")]
public List<PointF> Points { get; set; }
}
然后你必须重新设计你的财产:
[XmlArray("Polygons")]
[XmlArrayItem("Polygon")]
public List<Polygon> lstPolygons { get; set; }
如果您没有像本例中那样重新设计该属性的奢侈品,下面您可以找到一个可以正常工作的黑客。它不是清洁,但它很有效,而且很简单。
我们的想法是XmlIgnore您当前的属性,以防止XML序列化程序处理它,然后使用另一个仅用于序列化的getter,它将使用LINQ动态重新格式化数据,以便为序列化器做好准备。看看代码:
[XmlRoot("AlertInfo")]
public class AlertInfo
{
[XmlElement("TargetID")]
public string strTargetId { get; set; }
[XmlElement("ChannelID")]
public string strChId { get; set; }
[XmlElement("Timestamp")]
public string strTimestamp { get; set; }
[XmlElement("Object")]
public RectObject rfObject { get; set; }
[XmlIgnore]
public List<List<PointF[]>> lstPolygons { get; set; }
[XmlArray("Polygons")]
[XmlArrayItem("Polygon")]
public List<Polygon> Polygons
{
get {
return lstPolygons.Select(p => new Polygon() { Points = p.SelectMany(lp => lp).ToList() }).ToList();
}
}
}
序列化上述AlertInfo Polygons属性的结果如下:
<Polygons>
<Polygon>
<Point x="0" y="0" />
<Point x="0" y="0" />
</Polygon>
</Polygons>
另一种方法是实现IXmlSerializable并自己控制序列化。看看这篇文章如何做到:
http://www.codeproject.com/Articles/43237/How-to-Implement-IXmlSerializable-Correctly
答案 1 :(得分:0)
你嵌套了很多,删除了最嵌套的数组:
public List<List<OVReady.Types.PointF[]>> lstPolygons { get; set; }
到
public List<List<OVReady.Types.PointF>> lstPolygons { get; set; }