2016-06-03 2 views
0

Die XML-Datei ist wie folgtWie Deserialisieren ich diese XML zurück in ein Array von Punktobjekten

<?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> 

ich diese XML zurück in Polygon-Objekte deserialisiert werden soll. Meine Polygon-Klasse ist als

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

Mein Deserialisierung Code

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

ist habe ich bisher eine Abhilfe geschaffen durch eine Point2D Klasse mit X und Y Erstellen Attribute und dann Erstellen von Point-Objekten mit ihnen. Gibt es eine Möglichkeit, die unter Point2D aufgelisteten Attribute direkt den Point-Objekten wie pointObject.X und pointObject.Y zuzuordnen?

+0

Wie sieht Ihre Point-Klasse aus? –

+0

Das in System.Drawing –

Antwort

1

Die obige XML in die System.Drawing.Point Struktur und die Klasse deserialisiert werden

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

wie folgt:

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

schnellste Lösung wäre xml.linq zu verwenden, zum Beispiel, was Sie tun können, ist

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") 
    }) 
}); 
+0

vordefinierte Polygon-Objekt enthält ein Array von Point2D-Objekten. Wird dieser Ansatz Punkte in Arrays deserialisieren? –

1

Sie diese Klasse verwenden können für Ihre xml deserialisieren.

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

Hier ist der Code für die Klasse mit der XML-Füllung

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

OP möchte eine systemeigene 'System.Drawing.Point' Klasse verwenden. –

+0

Danke für die Mühe, aber ich habe bereits Deserialisierung zu einer Point2D-Klasse gemacht. Ich wollte wissen, ob es direkt in die Point-Klasse deserialisiert werden kann. –

+0

Ahh, dann habe ich missverstanden, mein Schlechter. – Sondre

0

Sie können wie folgt durchführen.

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

und verwenden diese Klasse wie folgt ..

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