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

时间:2016-06-03 05:34:46

标签: 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" 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>

我想将此XML反序列化为Polygon对象。我的Polygon类如下

[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类然后使用它们创建Point对象来管理变通方法。有没有办法直接将Point2D下列出的属性分配给像pointObject.XpointObject.Y这样的Point对象?

4 个答案:

答案 0 :(得分:1)

最快的解决方案是使用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")
    })
});

答案 1 :(得分:1)

您可以使用此类对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;
            }
        }
    }

以下是使用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();

答案 2 :(得分:1)

上述XML可以反序列化为System.Drawing.Point结构和类

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

答案 3 :(得分:0)

你可以按照以下方式做。

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

并像这样使用这个类..

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