这是我班级的方式
class SerClass
{
Line[] Line;
Circle[] Circle;
Square[] Square;
}
这就是我的XML出现的方式:
<Line .../>
<Line .../>
<Circle .../>
<Circle .../>
<Square .../>
<Square .../>
我的代码应该如何才能指定元素的顺序,例如:
<Square .../>
<Line .../>
<Circle .../>
<Line .../>
<Circle .../>
<Square .../>
答案 0 :(得分:0)
这就是所谓的话题。我添加了一个名为GeometryCollection的新类,它有一个Line,Square和Circle对象,然后在SerClass中有一个名为GeoCollection的GeometryCollection对象数组,当序列化时产生下面的输出,其中Circle,Square和Line被分组在GeometryCollection元素。
让我知道这样的事情是否适合您的需求,或者如果有任何问题,您可能无法使用,我可以尝试建议做什么,但据我所知,这会将Line,Square和Circle对象组合在一起的最佳方法,如果这就是你想要的。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace CommandLineProgram.Miscellaneous
{
public class SerializeSerClass
{
public static void Main()
{
XmlSerializer ser = new XmlSerializer(typeof(SerClass));
using (StringWriter writer = new StringWriter())
{
ser.Serialize(writer, new SerClass());
Console.WriteLine(writer.ToString());
}
}
}
[Serializable]
public class Line
{
public string Name { get; set; }
public Line()
{
Name = "Line";
}
}
[Serializable]
public class Circle
{
public string Name { get; set; }
public Circle()
{
Name = "Circle";
}
}
[Serializable]
public class Square
{
public string Name { get; set; }
public Square()
{
Name = "Square";
}
}
[Serializable]
public class SerClass
{
public GeometryCollection[] GeoCollection { get; set; }
public SerClass()
{
GeoCollection = new GeometryCollection[]
{
new GeometryCollection()
{
Circle = new Circle(),
Square = new Square(),
Line = new Line()
},
new GeometryCollection()
{
Circle = new Circle(),
Square = new Square(),
Line = new Line()
},
new GeometryCollection()
{
Circle = new Circle(),
Square = new Square(),
Line = new Line()
}
};
}
}
[Serializable]
public class GeometryCollection
{
public Line Line { get; set; }
public Square Square { get; set; }
public Circle Circle { get; set; }
}
}
<?xml version="1.0" encoding="utf-16"?>
<SerClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<GeoCollection>
<GeometryCollection>
<Line>
<Name>Line</Name>
</Line>
<Square>
<Name>Square</Name>
</Square>
<Circle>
<Name>Circle</Name>
</Circle>
</GeometryCollection>
<GeometryCollection>
<Line>
<Name>Line</Name>
</Line>
<Square>
<Name>Square</Name>
</Square>
<Circle>
<Name>Circle</Name>
</Circle>
</GeometryCollection>
<GeometryCollection>
<Line>
<Name>Line</Name>
</Line>
<Square>
<Name>Square</Name>
</Square>
<Circle>
<Name>Circle</Name>
</Circle>
</GeometryCollection>
</GeoCollection>
</SerClass>