G
Guest
Normally I would add attributes to the class to help the XmlSerializer to
deserialize the data which I've included below:
using System;
using System.Xml;
using System.Xml.Serialization;
public class XmlDeserializationDemo
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
private static void Main(string[] args)
{
Bead b = Deserialize(@"..\..\Bead.xml");
PrintBead(b);
}
private static void PrintBead(Bead b)
{
Console.WriteLine("Bead BeadID = {0}", b.BeadID);
foreach(Point point in b.Points)
{
Console.WriteLine("\tPoint x={0} y={1}", point.X, point.Y);
}
}
private static Bead Deserialize(string fileName)
{
XmlTextReader reader = new XmlTextReader(fileName);
XmlSerializer serializer = new XmlSerializer(typeof(Bead));
return serializer.Deserialize(reader) as Bead;
}
}
[XmlRoot("BeadType", Namespace="", IsNullable=false)]
public class Bead
{
private Point[] m_points;
private double m_id;
public Bead()
{
}
[XmlAttribute("BeadID")]
public double BeadID
{
get { return m_id; }
set { m_id = value; }
}
[XmlElement("Point")]
public Point[] Points
{
get { return m_points; }
set { m_points = value; }
}
}
public class Point
{
private string m_x;
private string m_y;
public Point()
{
}
public Point(string x, string y)
{
this.X = x;
this.Y = y;
}
[XmlAttribute("X")]
public string X
{
get { return m_x; }
set { m_x = value; }
}
[XmlAttribute("Y")]
public string Y
{
get { return m_y; }
set { m_y = value; }
}
}
Hope that helps
deserialize the data which I've included below:
using System;
using System.Xml;
using System.Xml.Serialization;
public class XmlDeserializationDemo
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
private static void Main(string[] args)
{
Bead b = Deserialize(@"..\..\Bead.xml");
PrintBead(b);
}
private static void PrintBead(Bead b)
{
Console.WriteLine("Bead BeadID = {0}", b.BeadID);
foreach(Point point in b.Points)
{
Console.WriteLine("\tPoint x={0} y={1}", point.X, point.Y);
}
}
private static Bead Deserialize(string fileName)
{
XmlTextReader reader = new XmlTextReader(fileName);
XmlSerializer serializer = new XmlSerializer(typeof(Bead));
return serializer.Deserialize(reader) as Bead;
}
}
[XmlRoot("BeadType", Namespace="", IsNullable=false)]
public class Bead
{
private Point[] m_points;
private double m_id;
public Bead()
{
}
[XmlAttribute("BeadID")]
public double BeadID
{
get { return m_id; }
set { m_id = value; }
}
[XmlElement("Point")]
public Point[] Points
{
get { return m_points; }
set { m_points = value; }
}
}
public class Point
{
private string m_x;
private string m_y;
public Point()
{
}
public Point(string x, string y)
{
this.X = x;
this.Y = y;
}
[XmlAttribute("X")]
public string X
{
get { return m_x; }
set { m_x = value; }
}
[XmlAttribute("Y")]
public string Y
{
get { return m_y; }
set { m_y = value; }
}
}
Hope that helps
Zion Zadik said:Dear all,
I have a set of c# data classes which i need to fill their data from xml
files. serialization looks to be the best way to accomplish this task.
Since the data classes are compiled and i don't have control on the xml
structure, I tried using the xmlAttributeOverrides class, to instruct the
serializer.
I was able to override the attributes for the root element, but I'm having
problems understanding how to deserialize arrays.
I would be delitaed if you could take a look at the code, and try to figure
out what's wrong there.
Thanks in advance,
Zion.
The data class to be deserialized:
public class Bead
{
private Point[] m_points;
private double m_id;
public Bead(){}
public double BeadID
{
get{return m_id;}
set{m_id = value;}
}
public Point[] Points
{
get{return m_points;}
set{m_points = value;}
}
}
public class Point
{
private string m_x;
private string m_y;
public Point(){}
public Point(string x, string y)
{
this.X = x;
this.Y = y;
}
public string X
{
get {return m_x;}
set {m_x = value;}
}
public string Y
{
get {return m_y;}
set {m_y = value;}
}
}
The input xml:
<BeadType BeadID="17"><Point ID="1" X="1" Y="0.02026"/><Point ID="2" X="2"
Y="0.02172"/><Point ID="3" X="3" Y="0.0233"/></BeadType>
The deserializing function:
private void button1_Click(object sender, System.EventArgs e)
{
FileStream fs = new FileStream(txtFile.Text, FileMode.Open);
XmlReader reader = new XmlTextReader(fs);
Type type = typeof(Bead);
XmlAttributeOverrides attOverrides = new XmlAttributeOverrides();
//Define the root element in the XML
XmlAttributes beadAtts = new XmlAttributes();
beadAtts.XmlRoot = new XmlRootAttribute("BeadType");
attOverrides.Add(type, beadAtts);
//
XmlAttributes beadIdAtts = new XmlAttributes();
XmlAttributeAttribute beadIDAtt = new XmlAttributeAttribute();
beadIDAtt.DataType = "double";
beadIDAtt.AttributeName = "BeadID";
beadIdAtts.XmlAttribute = beadIDAtt;
attOverrides.Add(type, "BeadID", beadIdAtts);
XmlAttributes atts = new XmlAttributes();
//XmlArrayAttribute aa = new XmlArrayAttribute("Point");
//atts.XmlArray = aa;
//attOverrides.Add(type, "Points", atts);
XmlArrayItemAttribute pointArrayItem = new XmlArrayItemAttribute();
pointArrayItem.ElementName = "Point";
pointArrayItem.DataType = "Point";
atts.XmlArrayItems.Add(pointArrayItem);
attOverrides.Add(typeof(Point), "Points", atts);
XmlSerializer serializer = new XmlSerializer(type, attOverrides);
object obj = serializer.Deserialize(reader);
reader.Close();
}