簡體   English   中英

如何將XML反序列化為Point對象數組

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

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

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

您可以使用此類反序列化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();

上面的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);
}

您可以執行以下操作。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM