473,374 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,374 software developers and data experts.

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 1238
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
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...
5
by: Stuart Robertson | last post by:
I am trying to find a solution that will allow me to use XmlSerializer to serialize/deserialize a collection of objects where a given object is shared between two or more other objects, and not...
3
by: Mark Turner | last post by:
Help! I want to serialize mu C# objects uzing the xmlserializer class. It works well whem all my classes are flat, but when I use inheritence to split the data and implementation I get an...
1
by: Daniel Lim | last post by:
When using XmlSerializer, I notice that it does not normalize the single quote and double quote characters, i.e. does not change ' to &apos; and " to &quot. However, it does normalize other...
3
by: Cindy Liu | last post by:
Hi, I use XmlSerializer to serialize xml string. It escapes xml special charaters, like < and > to < and >. How can I stop XmlSerializer to escape these charaters? Thanks in advance!!! Cindy
12
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....
1
by: Lloyd Dupont | last post by:
I get an XmlDocument from an external source. I want to load it/map it to C# class. I'm creating classes as I learn about the format. many tag are yet unparsed, but that seems ok for the...
0
by: Ian Armitage | last post by:
Hi, I am having problems using the XmlSerializer from within a signed assembly. I have narrowed down the problem to a compilation error (CS0647) occuring when .NET tries to compile code...
5
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I am using XML serialization for the first time and I have noticed something unexpected. The object I am serializing contains a field private NumericSettings _numericSettings; public...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.