469,299 Members | 2,050 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,299 developers. It's quick & easy.

xmlSerializer & xmlAttrbuteOverrides

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

{

public MyBead() {}

public int BeadID;

}

The input xml:

<CurveGraphPoint><BeadType BeadID="17"/></CurveGraphPoint>

The deserializing function:

private void btnConvert_Click(object sender, System.EventArgs e)
{
Stream fs = new FileStream(txtFile.Text, FileMode.Open);
XmlReader reader = new XmlTextReader(fs);

Type type = typeof(MyBead);

XmlAttributeOverrides attOverrides = new XmlAttributeOverrides();
XmlAttributes attrs = new XmlAttributes();

//Define the root element in the XML
XmlRootAttribute ra = new XmlRootAttribute("CurveGraphPoint");
attrs.XmlRoot = ra;

XmlElementAttribute ea = new XmlElementAttribute("BeadType", typeof(MyBead));
attrs.XmlElements.Add(ea);
attOverrides.Add(type, attrs);

XmlSerializer serializer = new XmlSerializer(type, attOverrides);

object obj = serializer.Deserialize(reader);
reader.Close();
}

Nov 12 '05 #1
1 1127
Problem solved.

Thanks for everyone that tried to help.
"Zion Zadik" <zz****@012.net.il> wrote in message news:%2****************@TK2MSFTNGP14.phx.gbl...
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 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 MyBead

{

public MyBead() {}

public int BeadID;

}

The input xml:

<CurveGraphPoint><BeadType BeadID="17"/></CurveGraphPoint>

The deserializing function:

private void btnConvert_Click(object sender, System.EventArgs e)
{
Stream fs = new FileStream(txtFile.Text, FileMode.Open);
XmlReader reader = new XmlTextReader(fs);

Type type = typeof(MyBead);

XmlAttributeOverrides attOverrides = new XmlAttributeOverrides();
XmlAttributes attrs = new XmlAttributes();

//Define the root element in the XML
XmlRootAttribute ra = new XmlRootAttribute("CurveGraphPoint");
attrs.XmlRoot = ra;

XmlElementAttribute ea = new XmlElementAttribute("BeadType", typeof(MyBead));
attrs.XmlElements.Add(ea);
attOverrides.Add(type, attrs);

XmlSerializer serializer = new XmlSerializer(type, attOverrides);

object obj = serializer.Deserialize(reader);
reader.Close();
}

Nov 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Mark Turner | last post: by
3 posts views Thread by Cindy Liu | last post: by
1 post views Thread by Lloyd Dupont | last post: by
5 posts views Thread by =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.