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 xmlAttributeOve rrides 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"><Po int 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(o bject sender, System.EventArg s e)
{
FileStream fs = new FileStream(txtF ile.Text, FileMode.Open);
XmlReader reader = new XmlTextReader(f s);
Type type = typeof(Bead);
XmlAttributeOve rrides attOverrides = new XmlAttributeOve rrides();
//Define the root element in the XML
XmlAttributes beadAtts = new XmlAttributes() ;
beadAtts.XmlRoo t = new XmlRootAttribut e("BeadType") ;
attOverrides.Ad d(type, beadAtts);
//
XmlAttributes beadIdAtts = new XmlAttributes() ;
XmlAttributeAtt ribute beadIDAtt = new XmlAttributeAtt ribute();
beadIDAtt.DataT ype = "double";
beadIDAtt.Attri buteName = "BeadID";
beadIdAtts.XmlA ttribute = beadIDAtt;
attOverrides.Ad d(type, "BeadID", beadIdAtts);
XmlAttributes atts = new XmlAttributes() ;
//XmlArrayAttribu te aa = new XmlArrayAttribu te("Point");
//atts.XmlArray = aa;
//attOverrides.Ad d(type, "Points", atts);
XmlArrayItemAtt ribute pointArrayItem = new XmlArrayItemAtt ribute();
pointArrayItem. ElementName = "Point";
pointArrayItem. DataType = "Point";
atts.XmlArrayIt ems.Add(pointAr rayItem);
attOverrides.Ad d(typeof(Point) , "Points", atts);
XmlSerializer serializer = new XmlSerializer(t ype, attOverrides);
object obj = serializer.Dese rialize(reader) ;
reader.Close();
} 4 1708
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.Seri alization;
public class XmlDeserializat ionDemo
{
/// <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.WriteLi ne("Bead BeadID = {0}", b.BeadID);
foreach(Point point in b.Points)
{
Console.WriteLi ne("\tPoint x={0} y={1}", point.X, point.Y);
}
}
private static Bead Deserialize(str ing fileName)
{
XmlTextReader reader = new XmlTextReader(f ileName);
XmlSerializer serializer = new XmlSerializer(t ypeof(Bead));
return serializer.Dese rialize(reader) as Bead;
}
}
[XmlRoot("BeadTy pe", Namespace="", IsNullable=fals e)]
public class Bead
{
private Point[] m_points;
private double m_id;
public Bead()
{
}
[XmlAttribute("B eadID")]
public double BeadID
{
get { return m_id; }
set { m_id = value; }
}
[XmlElement("Poi nt")]
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" wrote: 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 xmlAttributeOve rrides 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"><Po int 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(o bject sender, System.EventArg s e) { FileStream fs = new FileStream(txtF ile.Text, FileMode.Open); XmlReader reader = new XmlTextReader(f s);
Type type = typeof(Bead);
XmlAttributeOve rrides attOverrides = new XmlAttributeOve rrides();
//Define the root element in the XML XmlAttributes beadAtts = new XmlAttributes() ; beadAtts.XmlRoo t = new XmlRootAttribut e("BeadType") ; attOverrides.Ad d(type, beadAtts);
// XmlAttributes beadIdAtts = new XmlAttributes() ; XmlAttributeAtt ribute beadIDAtt = new XmlAttributeAtt ribute(); beadIDAtt.DataT ype = "double"; beadIDAtt.Attri buteName = "BeadID"; beadIdAtts.XmlA ttribute = beadIDAtt; attOverrides.Ad d(type, "BeadID", beadIdAtts);
XmlAttributes atts = new XmlAttributes() ; //XmlArrayAttribu te aa = new XmlArrayAttribu te("Point"); //atts.XmlArray = aa; //attOverrides.Ad d(type, "Points", atts);
XmlArrayItemAtt ribute pointArrayItem = new XmlArrayItemAtt ribute(); pointArrayItem. ElementName = "Point"; pointArrayItem. DataType = "Point"; atts.XmlArrayIt ems.Add(pointAr rayItem); attOverrides.Ad d(typeof(Point) , "Points", atts);
XmlSerializer serializer = new XmlSerializer(t ype, attOverrides);
object obj = serializer.Dese rialize(reader) ; reader.Close();
}
Thanks for your reply.
I'm sorry if I wasn't clear enough.
I don't want to add attributes in my class code. I only want to deserialize
the Xml using Attribute Overrides.
This way I don't need to re-compile my data classes with each change to the
XML (Which I don't have any control of).
Thanks,
Zion.
"Jorge Matos" <matos_jorge(NO SPAM)@hotmail.c om> wrote in message
news:EA******** *************** ***********@mic rosoft.com... 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.Seri alization;
public class XmlDeserializat ionDemo { /// <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.WriteLi ne("Bead BeadID = {0}", b.BeadID); foreach(Point point in b.Points) { Console.WriteLi ne("\tPoint x={0} y={1}", point.X, point.Y); } }
private static Bead Deserialize(str ing fileName) { XmlTextReader reader = new XmlTextReader(f ileName); XmlSerializer serializer = new XmlSerializer(t ypeof(Bead)); return serializer.Dese rialize(reader) as Bead; } }
[XmlRoot("BeadTy pe", Namespace="", IsNullable=fals e)] public class Bead { private Point[] m_points; private double m_id;
public Bead() { }
[XmlAttribute("B eadID")] public double BeadID { get { return m_id; } set { m_id = value; } }
[XmlElement("Poi nt")] 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" wrote:
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 xmlAttributeOve rrides 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"><Po int 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(o bject sender, System.EventArg s e) { FileStream fs = new FileStream(txtF ile.Text, FileMode.Open); XmlReader reader = new XmlTextReader(f s);
Type type = typeof(Bead);
XmlAttributeOve rrides attOverrides = new XmlAttributeOve rrides();
//Define the root element in the XML XmlAttributes beadAtts = new XmlAttributes() ; beadAtts.XmlRoo t = new XmlRootAttribut e("BeadType") ; attOverrides.Ad d(type, beadAtts);
// XmlAttributes beadIdAtts = new XmlAttributes() ; XmlAttributeAtt ribute beadIDAtt = new XmlAttributeAtt ribute(); beadIDAtt.DataT ype = "double"; beadIDAtt.Attri buteName = "BeadID"; beadIdAtts.XmlA ttribute = beadIDAtt; attOverrides.Ad d(type, "BeadID", beadIdAtts);
XmlAttributes atts = new XmlAttributes() ; //XmlArrayAttribu te aa = new XmlArrayAttribu te("Point"); //atts.XmlArray = aa; //attOverrides.Ad d(type, "Points", atts);
XmlArrayItemAtt ribute pointArrayItem = new XmlArrayItemAtt ribute(); pointArrayItem. ElementName = "Point"; pointArrayItem. DataType = "Point"; atts.XmlArrayIt ems.Add(pointAr rayItem); attOverrides.Ad d(typeof(Point) , "Points", atts);
XmlSerializer serializer = new XmlSerializer(t ype, attOverrides);
object obj = serializer.Dese rialize(reader) ; reader.Close();
}
Gotcha!
Here you go:
using System;
using System.Xml;
using System.Xml.Seri alization;
public class XmlDeserializat ionDemo
{
/// <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.WriteLi ne("Bead BeadID = {0}", b.BeadID);
foreach(Point point in b.Points)
{
Console.WriteLi ne("\tPoint x={0} y={1}", point.X, point.Y);
}
}
private static Bead Deserialize(str ing fileName)
{
XmlTextReader reader = new XmlTextReader(f ileName);
XmlAttributeOve rrides overrides = new XmlAttributeOve rrides();
XmlAttributes beadClassAttrib utes = new XmlAttributes() ;
XmlAttributes beadIDAttribute s = new XmlAttributes() ;
XmlAttributes pointsArrayAttr ibutes = new XmlAttributes() ;
XmlAttributes pointClassXAttr ibutes = new XmlAttributes() ;
pointClassXAttr ibutes.XmlAttri bute = new XmlAttributeAtt ribute("X");
XmlAttributes pointClassYAttr ibutes = new XmlAttributes() ;
pointClassYAttr ibutes.XmlAttri bute = new XmlAttributeAtt ribute("Y");
beadClassAttrib utes.XmlRoot = new XmlRootAttribut e("BeadType") ;
beadIDAttribute s.XmlAttribute = new XmlAttributeAtt ribute("BeadID" );
pointsArrayAttr ibutes.XmlEleme nts.Add(new XmlElementAttri bute("Point"));
overrides.Add(t ypeof(Bead), beadClassAttrib utes);
overrides.Add(t ypeof(Bead), "BeadID", beadIDAttribute s);
overrides.Add(t ypeof(Bead), "Points", pointsArrayAttr ibutes);
overrides.Add(t ypeof(Point), "X", pointClassXAttr ibutes);
overrides.Add(t ypeof(Point), "Y", pointClassYAttr ibutes);
XmlSerializer serializer = new XmlSerializer(t ypeof(Bead), overrides);
return serializer.Dese rialize(reader) as Bead;
}
}
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; }
}
}
Hope that helps!
"Zion Zadik" wrote: Thanks for your reply.
I'm sorry if I wasn't clear enough. I don't want to add attributes in my class code. I only want to deserialize the Xml using Attribute Overrides. This way I don't need to re-compile my data classes with each change to the XML (Which I don't have any control of).
Thanks, Zion.
"Jorge Matos" <matos_jorge(NO SPAM)@hotmail.c om> wrote in message news:EA******** *************** ***********@mic rosoft.com... 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.Seri alization;
public class XmlDeserializat ionDemo { /// <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.WriteLi ne("Bead BeadID = {0}", b.BeadID); foreach(Point point in b.Points) { Console.WriteLi ne("\tPoint x={0} y={1}", point.X, point.Y); } }
private static Bead Deserialize(str ing fileName) { XmlTextReader reader = new XmlTextReader(f ileName); XmlSerializer serializer = new XmlSerializer(t ypeof(Bead)); return serializer.Dese rialize(reader) as Bead; } }
[XmlRoot("BeadTy pe", Namespace="", IsNullable=fals e)] public class Bead { private Point[] m_points; private double m_id;
public Bead() { }
[XmlAttribute("B eadID")] public double BeadID { get { return m_id; } set { m_id = value; } }
[XmlElement("Poi nt")] 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" wrote:
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 xmlAttributeOve rrides 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"><Po int 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(o bject sender, System.EventArg s e) { FileStream fs = new FileStream(txtF ile.Text, FileMode.Open); XmlReader reader = new XmlTextReader(f s);
Type type = typeof(Bead);
XmlAttributeOve rrides attOverrides = new XmlAttributeOve rrides();
//Define the root element in the XML XmlAttributes beadAtts = new XmlAttributes() ; beadAtts.XmlRoo t = new XmlRootAttribut e("BeadType") ; attOverrides.Ad d(type, beadAtts);
// XmlAttributes beadIdAtts = new XmlAttributes() ; XmlAttributeAtt ribute beadIDAtt = new XmlAttributeAtt ribute(); beadIDAtt.DataT ype = "double"; beadIDAtt.Attri buteName = "BeadID"; beadIdAtts.XmlA ttribute = beadIDAtt; attOverrides.Ad d(type, "BeadID", beadIdAtts);
XmlAttributes atts = new XmlAttributes() ; //XmlArrayAttribu te aa = new XmlArrayAttribu te("Point"); //atts.XmlArray = aa; //attOverrides.Ad d(type, "Points", atts);
XmlArrayItemAtt ribute pointArrayItem = new XmlArrayItemAtt ribute(); pointArrayItem. ElementName = "Point"; pointArrayItem. DataType = "Point"; atts.XmlArrayIt ems.Add(pointAr rayItem); attOverrides.Ad d(typeof(Point) , "Points", atts);
XmlSerializer serializer = new XmlSerializer(t ype, attOverrides);
object obj = serializer.Dese rialize(reader) ; reader.Close();
}
Thanks Jorge!
Now everything looks so easy and obvious :-)
I really appreciate you spending the time on this,
Zion.
"Jorge Matos" <matos_jorge(NO SPAM)@hotmail.c om> wrote in message
news:9F******** *************** ***********@mic rosoft.com... Gotcha!
Here you go:
using System; using System.Xml; using System.Xml.Seri alization;
public class XmlDeserializat ionDemo { /// <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.WriteLi ne("Bead BeadID = {0}", b.BeadID); foreach(Point point in b.Points) { Console.WriteLi ne("\tPoint x={0} y={1}", point.X, point.Y); } }
private static Bead Deserialize(str ing fileName) { XmlTextReader reader = new XmlTextReader(f ileName);
XmlAttributeOve rrides overrides = new XmlAttributeOve rrides();
XmlAttributes beadClassAttrib utes = new XmlAttributes() ; XmlAttributes beadIDAttribute s = new XmlAttributes() ; XmlAttributes pointsArrayAttr ibutes = new XmlAttributes() ;
XmlAttributes pointClassXAttr ibutes = new XmlAttributes() ; pointClassXAttr ibutes.XmlAttri bute = new XmlAttributeAtt ribute("X");
XmlAttributes pointClassYAttr ibutes = new XmlAttributes() ; pointClassYAttr ibutes.XmlAttri bute = new XmlAttributeAtt ribute("Y");
beadClassAttrib utes.XmlRoot = new XmlRootAttribut e("BeadType") ; beadIDAttribute s.XmlAttribute = new XmlAttributeAtt ribute("BeadID" ); pointsArrayAttr ibutes.XmlEleme nts.Add(new XmlElementAttri bute("Point"));
overrides.Add(t ypeof(Bead), beadClassAttrib utes); overrides.Add(t ypeof(Bead), "BeadID", beadIDAttribute s); overrides.Add(t ypeof(Bead), "Points", pointsArrayAttr ibutes); overrides.Add(t ypeof(Point), "X", pointClassXAttr ibutes); overrides.Add(t ypeof(Point), "Y", pointClassYAttr ibutes);
XmlSerializer serializer = new XmlSerializer(t ypeof(Bead), overrides);
return serializer.Dese rialize(reader) as Bead; } }
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; } } }
Hope that helps!
"Zion Zadik" wrote:
Thanks for your reply.
I'm sorry if I wasn't clear enough. I don't want to add attributes in my class code. I only want to deserialize the Xml using Attribute Overrides. This way I don't need to re-compile my data classes with each change to the XML (Which I don't have any control of).
Thanks, Zion.
"Jorge Matos" <matos_jorge(NO SPAM)@hotmail.c om> wrote in message news:EA******** *************** ***********@mic rosoft.com... > 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.Seri alization; > > public class XmlDeserializat ionDemo > { > /// <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.WriteLi ne("Bead BeadID = {0}", b.BeadID); > foreach(Point point in b.Points) > { > Console.WriteLi ne("\tPoint x={0} y={1}", point.X, point.Y); > } > } > > private static Bead Deserialize(str ing fileName) > { > XmlTextReader reader = new XmlTextReader(f ileName); > XmlSerializer serializer = new XmlSerializer(t ypeof(Bead)); > return serializer.Dese rialize(reader) as Bead; > } > } > > [XmlRoot("BeadTy pe", Namespace="", IsNullable=fals e)] > public class Bead > { > private Point[] m_points; > private double m_id; > > public Bead() > { > } > > [XmlAttribute("B eadID")] > public double BeadID > { > get { return m_id; } > set { m_id = value; } > } > > [XmlElement("Poi nt")] > 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" wrote: > >> 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 xmlAttributeOve rrides 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"><Po int 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(o bject sender, System.EventArg s e) >> { >> FileStream fs = new FileStream(txtF ile.Text, FileMode.Open); >> XmlReader reader = new XmlTextReader(f s); >> >> Type type = typeof(Bead); >> >> XmlAttributeOve rrides attOverrides = new XmlAttributeOve rrides(); >> >> //Define the root element in the XML >> XmlAttributes beadAtts = new XmlAttributes() ; >> beadAtts.XmlRoo t = new XmlRootAttribut e("BeadType") ; >> attOverrides.Ad d(type, beadAtts); >> >> // >> XmlAttributes beadIdAtts = new XmlAttributes() ; >> XmlAttributeAtt ribute beadIDAtt = new XmlAttributeAtt ribute(); >> beadIDAtt.DataT ype = "double"; >> beadIDAtt.Attri buteName = "BeadID"; >> beadIdAtts.XmlA ttribute = beadIDAtt; >> attOverrides.Ad d(type, "BeadID", beadIdAtts); >> >> >> XmlAttributes atts = new XmlAttributes() ; >> //XmlArrayAttribu te aa = new XmlArrayAttribu te("Point"); >> //atts.XmlArray = aa; >> //attOverrides.Ad d(type, "Points", atts); >> >> XmlArrayItemAtt ribute pointArrayItem = new XmlArrayItemAtt ribute(); >> pointArrayItem. ElementName = "Point"; >> pointArrayItem. DataType = "Point"; >> atts.XmlArrayIt ems.Add(pointAr rayItem); >> attOverrides.Ad d(typeof(Point) , "Points", atts); >> >> XmlSerializer serializer = new XmlSerializer(t ype, attOverrides); >> >> object obj = serializer.Dese rialize(reader) ; >> reader.Close(); >> >> } >> >> >> >> >> >> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Zion Zadik |
last post by:
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.
|
by: Anthony Bouch |
last post by:
Hi
I've been reading using the XmlSerializer with custom collections.
I've discovered that when serializing a custom collection (a class that
implements ICollection, IList etc.) the XmlSerializer will only serialize
the collection items - with the default root as ArrayofMyItems etc.
My custom collection class has some additional public properties that I
would like to include in the serialization above the items element array (in
|
by: Bob Rock |
last post by:
Hello,
when serializing an array of elements of a class Classname using
XmlSerializer.Serialize() I get an XML like the following:
<?xml version="1.0">
<ArrayOfClassname>
.......
.......
</ArrayOfClassname>
|
by: Jamus Sprinson |
last post by:
Before I continue, I'm going to begin by saying I'm not by any means
an expert- I've been using .NET with C# for about 4 months now, and
basically just learning by example and docs.
A game project I work on uses Xml serialization to store game objects
for loading. One of the techniques we use is to make an array
property and use the set block to perform actions on the values after
they're loaded from the xml, as in:
public string Ids
|
by: SJD |
last post by:
I've just read Christoph Schittko's article on XmlSerializer:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnxmlnet/html/trblshtxsd.asp
. . . and very informative it is too.
I, too, am getting nasty FileNotFound exceptions. I've read, and
digested the article, and I think I've found a bug -- it's difficult to
track, though it does happen often.
| |
by: Don McNamara |
last post by:
Hi,
I've hit quite a strange problem with XmlSerializer on my W2K3 server. When
I serialize/deserialize using an exe on my local computer (XP), everything
works fine. When I put the code out on the server (W2K3) it throws an
exception. It only seems to happen when serializing/deserializing _arrays_
of a type. If I just serialize/deserialize one instance, it works fine.
The exception I get is: (sorry for the word wrapping.)...
|
by: Zion Zadik |
last post by:
Dear all,
I have a set of c# data classes wich 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. Looks like I'm doing something basically wrong, since I fail to do that even with a very simple example.
I would be delitaed if...
|
by: Daniel Lindros via .NET 247 |
last post by:
Hi!
I am currently trying to learn C#, and have started by developinga simple messaging application with a client and server. Byusing callback delegates, I can asynchronously send and recievetext as byte arrays.
Because I wanted to be able to send more advanced messagesbetween the client and server, I decided to create my ownmessage class, serialize it, and deserialize it at the other end(using XmlSerializer).
Now to my question:
...
|
by: Phil Galey |
last post by:
I'm using XMLSerializer to serialize a hierarchy of class-based objects to
XML. So far, this is working successfully by employing arrays to handle the
multiplicity of child objects. However, I'm trying to employ the use of
collection classes instead because there are methods I need to implement at
that level. But when I instantiate the XMLSerializer, it gives the error:
There was an error reflecting <Root Type>. If I try using
...
|
by: kimtherkelsen |
last post by:
Hi,
I want to send XML data from a server to some clients over a network
connection using the TCP/IP protocol. If I send the XMLs as byte
arrays I need to insert header information in the data to distinguish
the XMLs from each other in the stream of data. Is there any way to
avoid this (for instance by sending SOAP telegrams))?
I have tried using the XMLSerializer.Serialize(stream) to serialize
the XML telegrams and send them over the...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |