【问题标题】:How do I deserialize this XML back into an array of Point objects如何将此 XML 反序列化回 Point 对象数组
【发布时间】:2016-06-03 05:34:46
【问题描述】:

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 对象。我的多边形类如下

[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 下列出的属性分配给 Point 对象,如 pointObject.XpointObject.Y

【问题讨论】:

  • 你的 Point 类是什么样子的?
  • 它在 System.Drawing 中预定义

标签: c# xml


【解决方案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);
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • Polygon 对象包含一个 Point2D 对象数组,这种方法会将 Points 反序列化为数组吗?
    【解决方案3】:

    你可以使用这个类来反序列化你的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();
    

    【讨论】:

    • OP 想要使用原生的System.Drawing.Point 类。
    • 感谢您的努力,但我已经完成了对 Point2D 类的反序列化。我想知道它是否可以直接反序列化为Point类。
    • 啊,那我误会了,我的错。
    【解决方案4】:

    您可以按照以下方式操作。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-20
      • 1970-01-01
      • 2018-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多