我有一個嵌套列表在C#:List<List<OVReady.Types.PointF[]>>
,它必須是序列化到XML在C#中序列化一個嵌套列表,以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>
^h我可以移除標籤,並根據需要設置位置x和y嗎?
您需要實現'Serialize'和你的'AlertInfo'類'Deserialize'方法來實現這一 – 2014-09-26 12:12:43
您可以實現'IXmlSerializable'和處理序列和'ReadXml'和'WriteXml'方法中的反序列化。 – juharr 2014-09-26 12:13:50
Juharr的好處。我不認爲你只能通過屬性來處理它。爲了實現IXmlSerializable正確檢出:http://www.codeproject.com/Articles/43237/How-to-Implement-IXmlSerializable-Correctly – 2014-09-26 12:16:01