简体   繁体   English

如何将XML反序列化为Point对象数组

[英]How do I deserialize this XML back into an array of Point objects

The XML file is as follows 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="0" Y="0" />
      <Point2D X="100" Y="0" />
      <Point2D X="100" Y="200" />
      <Point2D X="0" Y="200" />
    </Points>
  </Polygon>
</Polygons>

I want to deserialize this XML back into Polygon objects. 我想将该XML反序列化为Polygon对象。 My Polygon class is as follows 我的多边形课如下

[XmlType("Polygon")]
public class Polygon
{
    [XmlElement("Points")]
    public Point[] points { get; set; }
}

My deserialization code is 我的反序列化代码是

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();

I've so far managed a workaround by creating a Point2D class with X and Y attributes and then creating Point objects using them. 到目前为止,我已经通过创建具有X和Y属性的Point2D类,然后使用它们创建Point对象来管理一种变通方法。 Is there any way to directly assign the attributes listed under Point2D to Point objects like pointObject.X and pointObject.Y ? 有什么方法可以将Point2D下列出的属性直接分配给Point对象(如pointObject.XpointObject.Y

Fastest solution would be to use xml.linq, for instance what you can do is 最快的解决方案是使用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")
    })
});

You can use this class for deserialize your xml. 您可以使用此类反序列化xml。

 [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class Polygons
    {

        private Polygon[] _field;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("Polygon")]
        public Polygon[] Polygon
        {
            get
            {
                return this._field;
            }
            set
            {
                this._field = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class Polygon
    {

        private Point2D[] pointsField;

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayItemAttribute("Point2D", IsNullable = false)]
        public Point2D[] Points
        {
            get
            {
                return this.pointsField;
            }
            set
            {
                this.pointsField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class Point2D
    {

        private byte xField;

        private byte yField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public byte X
        {
            get
            {
                return this.xField;
            }
            set
            {
                this.xField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public byte Y
        {
            get
            {
                return this.yField;
            }
            set
            {
                this.yField = value;
            }
        }
    }

Here is the code for filling the class with your XML 这是用您的XML填充类的代码

Polygons polygons = null;
string path = "poligons.xml";

XmlSerializer serializer = new XmlSerializer(typeof(Polygons));

StreamReader reader = new StreamReader(path);
cars = (Polygons)serializer.Deserialize(reader);
reader.Close();

The above XML can be deserialized into the System.Drawing.Point structure and the class 上面的XML可以反序列化为System.Drawing.Point结构和类

public class Polygon
{
    [XmlArrayItem("Point2D")]
    public Point[] Points { get; set; }
}

as follows: 如下:

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);
}

You can Do as Follows. 您可以执行以下操作。

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; }
}

and use this Class like this.. 并像这样使用这个类。

 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);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM