473,498 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

xmlSerializer & xmlAttrbuteOverrides of arrays

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

}

Nov 17 '05 #1
4 1696
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

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

}


Nov 17 '05 #2
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(NOSPAM)@hotmail.com> wrote in message
news:EA**********************************@microsof t.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.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" 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 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();

}


Nov 17 '05 #3
Gotcha!

Here you go:

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

XmlAttributeOverrides overrides = new XmlAttributeOverrides();

XmlAttributes beadClassAttributes = new XmlAttributes();
XmlAttributes beadIDAttributes = new XmlAttributes();
XmlAttributes pointsArrayAttributes = new XmlAttributes();

XmlAttributes pointClassXAttributes = new XmlAttributes();
pointClassXAttributes.XmlAttribute = new XmlAttributeAttribute("X");

XmlAttributes pointClassYAttributes = new XmlAttributes();
pointClassYAttributes.XmlAttribute = new XmlAttributeAttribute("Y");

beadClassAttributes.XmlRoot = new XmlRootAttribute("BeadType");
beadIDAttributes.XmlAttribute = new XmlAttributeAttribute("BeadID");
pointsArrayAttributes.XmlElements.Add(new XmlElementAttribute("Point"));

overrides.Add(typeof(Bead), beadClassAttributes);
overrides.Add(typeof(Bead), "BeadID", beadIDAttributes);
overrides.Add(typeof(Bead), "Points", pointsArrayAttributes);
overrides.Add(typeof(Point), "X", pointClassXAttributes);
overrides.Add(typeof(Point), "Y", pointClassYAttributes);

XmlSerializer serializer = new XmlSerializer(typeof(Bead), overrides);

return serializer.Deserialize(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(NOSPAM)@hotmail.com> wrote in message
news:EA**********************************@microsof t.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.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" 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 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();

}



Nov 17 '05 #4
Thanks Jorge!
Now everything looks so easy and obvious :-)

I really appreciate you spending the time on this,
Zion.

"Jorge Matos" <matos_jorge(NOSPAM)@hotmail.com> wrote in message
news:9F**********************************@microsof t.com...
Gotcha!

Here you go:

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

XmlAttributeOverrides overrides = new XmlAttributeOverrides();

XmlAttributes beadClassAttributes = new XmlAttributes();
XmlAttributes beadIDAttributes = new XmlAttributes();
XmlAttributes pointsArrayAttributes = new XmlAttributes();

XmlAttributes pointClassXAttributes = new XmlAttributes();
pointClassXAttributes.XmlAttribute = new XmlAttributeAttribute("X");

XmlAttributes pointClassYAttributes = new XmlAttributes();
pointClassYAttributes.XmlAttribute = new XmlAttributeAttribute("Y");

beadClassAttributes.XmlRoot = new XmlRootAttribute("BeadType");
beadIDAttributes.XmlAttribute = new XmlAttributeAttribute("BeadID");
pointsArrayAttributes.XmlElements.Add(new XmlElementAttribute("Point"));

overrides.Add(typeof(Bead), beadClassAttributes);
overrides.Add(typeof(Bead), "BeadID", beadIDAttributes);
overrides.Add(typeof(Bead), "Points", pointsArrayAttributes);
overrides.Add(typeof(Point), "X", pointClassXAttributes);
overrides.Add(typeof(Point), "Y", pointClassYAttributes);

XmlSerializer serializer = new XmlSerializer(typeof(Bead), overrides);

return serializer.Deserialize(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(NOSPAM)@hotmail.com> wrote in message
news:EA**********************************@microsof t.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.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" 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 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();
>>
>> }
>>
>>
>>
>>
>>
>>


Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1604
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...
3
6985
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...
16
9486
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> ....... ..........
1
4603
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...
12
8447
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....
3
3143
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...
1
1245
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...
0
1645
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...
1
1707
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...
3
3288
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...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7208
jinu1996
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...
1
6890
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...
1
4915
isladogs
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...
0
3095
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...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1423
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 ...
1
657
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
292
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.