C# 如何将此XML反序列化回点对象数组
XML文件如下所示C# 如何将此XML反序列化回点对象数组,c#,xml,C#,Xml,XML文件如下所示 <?xml version="1.0" encoding="utf-8" ?> <Polygons> <Polygon> <Points> <Point2D X="0" Y="0" /> <Point2D X="100" Y="0" /> <Point2D X="100" Y="200" /> <Point2D X="0
<?xml version="1.0" encoding="utf-8" ?>
<Polygons>
<Polygon>
<Points>
<Point2D X="0" Y="0" />
<Point2D X="100" Y="0" />
<Point2D X="100" Y="200" />
<Point2D X="0" Y="200" />
</Points>
</Polygon>
<Polygon>
<Points>
<Point2D X="0" Y="0" />
<Point2D X="100" Y="0" />
<Point2D X="100" Y="200" />
<Point2D X="0" Y="200" />
</Points>
</Polygon>
</Polygons>
[XmlType("Polygon")]
public class Polygon
{
[XmlElement("Points")]
public Point[] points { get; set; }
}
我的反序列化代码是
XmlSerializer serializer = new XmlSerializer(typeof(Polygon[]),new XmlRootAttribute("Polygons"));
FileStream fs = new FileStream(filename, FileMode.Open);
XmlReader reader = XmlReader.Create(fs);
Polygon[] p;
p = (Polygon[])serializer.Deserialize(reader);
fs.Close();
到目前为止,我已经通过创建一个带有X和Y属性的
Point2D
类,然后使用它们创建点对象来管理解决方案。是否有任何方法可以直接将Point2D下列出的属性分配给点对象,如pointObject.X
和pointObject.Y
?最快的解决方案是使用xml.linq,例如,您可以做的是
var polygon = XDocument("Polygons>...</Polygons");
var polygonObject = polygon.Decendants("Polygon").Select(d=> new Polygon() {
Points = d.Decendants("Point2D").Select(a => new Point(){
X = a.Attribute("X"),
Y = a.Attribute("Y")
})
});
var polygon=XDocument(“Polygons>…最快的解决方案是使用xml.linq,例如,您可以做的是
var polygon = XDocument("Polygons>...</Polygons");
var polygonObject = polygon.Decendants("Polygon").Select(d=> new Polygon() {
Points = d.Decendants("Point2D").Select(a => new Point(){
X = a.Attribute("X"),
Y = a.Attribute("Y")
})
});
var polygon=XDocument(“Polygons>…您可以使用此类来反序列化xml
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace=”“,IsNullable=false)]
公共部分类多边形
{
私有多边形[]_字段;
///
[System.Xml.Serialization.XmlElementAttribute(“多边形”)]
公共多边形[]多边形
{
得到
{
返回此.\u字段;
}
设置
{
此._字段=值;
}
}
}
///
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
公共部分类多边形
{
私有点2d[]点域;
///
[System.Xml.Serialization.XmlArrayItemAttribute(“Point2D”,IsNullable=false)]
公共点2D[]点
{
得到
{
返回此.points字段;
}
设置
{
this.pointsField=值;
}
}
}
///
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
公共部分类Point2D
{
专用字节xField;
专用字节字段;
///
[System.Xml.Serialization.XmlAttributeAttribute()]
公共字节X
{
得到
{
返回此.xField;
}
设置
{
this.xField=值;
}
}
///
[System.Xml.Serialization.XmlAttributeAttribute()]
公共字节Y
{
得到
{
返回此.yField;
}
设置
{
this.yField=值;
}
}
}
下面是用XML填充类的代码
您可以使用这个类来反序列化xml
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace=”“,IsNullable=false)]
公共部分类多边形
{
私有多边形[]_字段;
///
[System.Xml.Serialization.XmlElementAttribute(“多边形”)]
公共多边形[]多边形
{
得到
{
返回此.\u字段;
}
设置
{
此._字段=值;
}
}
}
///
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
公共部分类多边形
{
私有点2d[]点域;
///
[System.Xml.Serialization.XmlArrayItemAttribute(“Point2D”,IsNullable=false)]
公共点2D[]点
{
得到
{
返回此.points字段;
}
设置
{
this.pointsField=值;
}
}
}
///
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
公共部分类Point2D
{
专用字节xField;
专用字节字段;
///
[System.Xml.Serialization.XmlAttributeAttribute()]
公共字节X
{
得到
{
返回此.xField;
}
设置
{
this.xField=值;
}
}
///
[System.Xml.Serialization.XmlAttributeAttribute()]
公共字节Y
{
得到
{
返回此.yField;
}
设置
{
this.yField=值;
}
}
}
下面是用XML填充类的代码
你可以这样做
public class Polygons
{
[XmlElement("Polygon")]
public List<Polygon> Polygon { get; set; }
}
public class Polygon
{
[XmlArrayItem]
public Point2D[] Points { get; set; }
}
public class Point2D
{
[XmlAttribute]
public int X { get; set; }
[XmlAttribute]
public int Y { get; set; }
}
公共类多边形
{
[XmlElement(“多边形”)]
公共列表多边形{get;set;}
}
公共类多边形
{
[XmlArrayItem]
公共点2d[]点{get;set;}
}
公共类Point2D
{
[XmlAttribute]
公共整数X{get;set;}
[XmlAttribute]
公共整数Y{get;set;}
}
然后像这样使用这个类
string xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> "+
"<Polygons> "+
"<Polygon> "+
"<Points> "+
"<Point2D X=\"0\" Y=\"0\" /> "+
"<Point2D X=\"100\" Y=\"0\" /> "+
"<Point2D X=\"100\" Y=\"200\" /> "+
"<Point2D X=\"0\" Y=\"200\" /> "+
"</Points> "+
"</Polygon> "+
"<Polygon> "+
"<Points> "+
"<Point2D X=\"44\" Y=\"0\" /> "+
"<Point2D X=\"100\" Y=\"0\" /> "+
"<Point2D X=\"100\" Y=\"200\" /> "+
"<Point2D X=\"0\" Y=\"200\" /> "+
"</Points> "+
"</Polygon> "+
"</Polygons> ";
XmlSerializer serializer = new XmlSerializer(typeof(Polygons));
using (TextReader reader = new StringReader(xml))
{
Polygons b = (Polygons)serializer.Deserialize(reader);
}
string xml=“”+
" "+
" "+
" "+
" "+
" "+
" "+
" "+
" "+
" "+
" "+
" "+
" "+
" "+
" "+
" "+
" "+
" "+
" ";
XmlSerializer序列化程序=新建
public class Polygon
{
[XmlArrayItem("Point2D")]
public Point[] Points { get; set; }
}
var attrX = new XmlAttributes { XmlAttribute = new XmlAttributeAttribute("X") };
var attrY = new XmlAttributes { XmlAttribute = new XmlAttributeAttribute("Y") };
var overrides = new XmlAttributeOverrides();
overrides.Add(typeof(Point), "X", attrX);
overrides.Add(typeof(Point), "Y", attrY);
var serializer = new XmlSerializer(
typeof(Polygon[]), overrides, null, new XmlRootAttribute("Polygons"), null);
Polygon[] polygons;
using (var fs = new FileStream(filename, FileMode.Open))
{
polygons = (Polygon[])serializer.Deserialize(fs);
}