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 1698
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...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
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...
| |