我正在尝试序列化多边形列表。这个类包括3个公共字段:amountOfVertexes,List where Stroke -is我自己的类,List其中Point - 是C#class。我不知道在哪里错误地将它序列化了? 这是我的班级
[XmlRoot("CustomPolygon")]
public class CustomPolygon
{
#region Cunstructors
public CustomPolygon()
{
}
#endregion
#region Public fields
[XmlAttribute("amountOfVertexes")]
public int amountOfVertexes; // Количество сторон полигона
[XmlArray("Points")]
[XmlArrayItem("Point")]
public List<Point> listOfVertexes; // Список всех вершин
[XmlArray("Strokes")]
[XmlArrayItem("Stroke")]
public List<Stroke> listOfStrokes; // Список ребер
#endregion
#region Private fields
private PointCollection Points;
#endregion
我在Stroke中添加了相同的元素
[XmlRoot("Stroke")]
public class Stroke
{
#region Constructors
public Stroke()
{
this.beginPoint = new Point();
this.endPoint = new Point();
}
public Stroke(Point pBegin, Point pEnd)
{
this.beginPoint = pBegin;
this.endPoint = pEnd;
}
#endregion
#region Public Member Variables
[XmlAttribute("beginPoint")]
public Point beginPoint; // Начальная точка отрезка
[XmlAttribute("endPoint")]
public Point endPoint; // Конечная точка отрезка
#endregion
}
但我无法向Point类添加相同的元素(我的意思是[XmlAttribute]),因为它已关闭。我做错了什么?我正在尝试序列化此代码的帮助:
using (StreamWriter writer = new StreamWriter(saveFileDialogPolygon.FileName))
{
XmlSerializer xmlSerializerCustom = new XmlSerializer(typeof (List <CustomPolygon>));
xmlSerializerCustom.Serialize(writer,listOfCustomPolygons);
}
writer.Close();
我得到了“反射类型发生错误”可能是因为Point的错误?那么,我如何序列化(和desirialze)标准的Point类?
答案 0 :(得分:0)
我过去遇到过类似的问题,请参阅: Build a Custom Serialization as String in System.Xml.Serialization。
我的解决方案适合你的情况将是这样的:
[XmlIgnore]
public List<Point> listOfVertexes;
[XmlArray("Points")]
[XmlArrayItem("Point")]
public string[] listOfVertexesString
{
get { return listOfVertexes.Select(s => s.X + "," + s.Y).ToArray(); }
set
{
listOfVertexes = value
.Select(s => new Point
{
X = int.Parse(s.Split(',')[0]),
Y = int.Parse(s.Split(',')[1])
})
.ToList();
}
}
这将生成这样的XML:
<!-- something before -->
<Points>
<Point>0,1</Point>
<Point>4,10</Point>
<Point>3,5</Point>
</Points>
<!-- something after -->
答案 1 :(得分:0)
非常感谢!我认为这是很好的体验!但问题不在于对象点的序列化!问题在于此代码
using (StreamReader reader = new StreamReader(openFileDialog.FileName))
{
// Найдем количество полигонов в Xml-файле
XmlDocument loadedXml = new XmlDocument();
string directory = openFileDialog.FileName;
loadedXml.Load(reader); **// I USE IT, BUT NO CLOSE!**
XmlNodeList polygonNodeList = loadedXml.GetElementsByTagName("CustomPolygon");
if (polygonNodeList.Count > 1)
for (int i = 0; i < polygonNodeList.Count; i++)
{
// List <CustomPolygon> listcp= new List<CustomPolygon>();
XmlSerializer xmlReader = new XmlSerializer(typeof (List<CustomPolygon>));
var listcp = (List<CustomPolygon>)xmlReader.Deserialize(reader);
}
else
{
XmlSerializer xmlReaderResult = new XmlSerializer((typeof (CustomPolygon)));
CustomPolygon loadedResultPolygon = (CustomPolygon) xmlReaderResult.Deserialize(reader); **// MORE OVER I USE IT IN THIS PLACE, WITHOUT CLOSE()** BEFORE
resultOfTwoPolygonsUnion = loadedResultPolygon;
}
reader.Close();
}
答案 2 :(得分:0)
而不是它,我应该这样使用:
StreamReader file=new StreamReader(openFileDialog.FileName);
// Найдем количество полигонов в Xml-файле
XmlDocument loadedXml = new XmlDocument();
loadedXml.Load(file);
XmlNodeList polygonNodeList = loadedXml.GetElementsByTagName("CustomPolygon");
file.Close();
using (StreamReader reader = new StreamReader(openFileDialog.FileName))
{
XmlSerializer xmlReader = new XmlSerializer(typeof(CustomPolygon));
CustomPolygon resultPolygon = new CustomPolygon();
resultPolygon = (CustomPolygon)xmlReader.Deserialize(reader);
}