All -
I'm pretty comfortable with simple XML serialization of objects. However I
observed something the other day and I wanted to know if I solved it the
right way.
Basically if I have a string which contains a single xml node, I can
serialize with this line of code:
object = (object)serializer.Deserialize(new
XmlTextReader(xml,XmlNodeType.Element,null));
where xml is the string that represents the node
However if the string contains two nodes that individually would serialize
to the "object" I have to do the following to be able to serialize to an
array. Code looks like this:
string xml = "<ArrayOfObject >" + xml
+ "</ArrayOfObject>";
object = (object[])serializer.Deserialize(new
XmlTextReader(xml,XmlNodeType.Element,null));
If don't concatenate "ArrayOfObject" elements to beginning and end of nodes,
it doesn work. I get the exception "...xmlns = ''" wasn't expected...".
Have I solved this problem correctly, or is there another way to get around
this.
ice 3 2194
Ice,
As you've seen, the XmlSerializer cannot deserialize XML Document
fragments with Arrays correctly -- and glancing through the Rotor source
[0], I couldn't find anything that would generate code that would be
what you are looking for.
Another solution avoiding the string concatenation would be to
deserialize one array item at a time like in the example below:
string xml =
"<MyClass><foo>foobar</foo></MyClass><MyClass><foo>bar</foo></MyClass>";
XmlSerializer ser = new XmlSerializer(typeof(MyClass));
XmlTextReader reader = new XmlTextReader(xml, XmlNodeType.Element,
null);
object o;
ArrayList list = new ArrayList();
MyClass[] objs;
while( false == reader.EOF )
{
o = ser.Deserialize(reader);
list.Add(o);
}
objs = list.ToArray(typeof(MyClass)) as MyClass[];
In the example, I store all the deserialized items in an ArrayList
first, to later convert that to a type array, but you may want to work
with strongly typed collections to avoid the type conversion. You can
create those collections by hand or using a tool like CodeSmith [1].
HTH,
Christoph Schittko
MS MVP XML http://weblogs.asp.net/cschittko
[0] http://www.microsoft.com/downloads/d...A1C93FA-7462-4
7D0-8E56-8DD34C6292F0&displaylang=en
[1] http://www.ericjsmith.net/codesmith/ -----Original Message----- From: Ice [mailto:ic*@nospam.com] Posted At: Thursday, November 25, 2004 10:08 PM Posted To: microsoft.public.dotnet.xml Conversation: xml serialization Subject: xml serialization
All -
I'm pretty comfortable with simple XML serialization of objects.
However I observed something the other day and I wanted to know if I solved it
the right way.
Basically if I have a string which contains a single xml node, I can serialize with this line of code:
object = (object)serializer.Deserialize(new XmlTextReader(xml,XmlNodeType.Element,null));
where xml is the string that represents the node
However if the string contains two nodes that individually would
serialize to the "object" I have to do the following to be able to serialize to
an array. Code looks like this:
string xml = "<ArrayOfObject >" + xml + "</ArrayOfObject>"; object = (object[])serializer.Deserialize(new XmlTextReader(xml,XmlNodeType.Element,null));
If don't concatenate "ArrayOfObject" elements to beginning and end of nodes, it doesn work. I get the exception "...xmlns = ''" wasn't
expected...". Have I solved this problem correctly, or is there another way to get around this. ice
thanks for the reply. your solution also works well. have you used
CodeSmith extensively?
ice
"Christoph Schittko [MVP]" <IN**********@austin.rr.com> wrote in message
news:Os**************@TK2MSFTNGP09.phx.gbl... Ice,
As you've seen, the XmlSerializer cannot deserialize XML Document fragments with Arrays correctly -- and glancing through the Rotor source [0], I couldn't find anything that would generate code that would be what you are looking for.
Another solution avoiding the string concatenation would be to deserialize one array item at a time like in the example below:
string xml = "<MyClass><foo>foobar</foo></MyClass><MyClass><foo>bar</foo></MyClass>"; XmlSerializer ser = new XmlSerializer(typeof(MyClass));
XmlTextReader reader = new XmlTextReader(xml, XmlNodeType.Element, null); object o; ArrayList list = new ArrayList(); MyClass[] objs;
while( false == reader.EOF ) { o = ser.Deserialize(reader); list.Add(o); } objs = list.ToArray(typeof(MyClass)) as MyClass[];
In the example, I store all the deserialized items in an ArrayList first, to later convert that to a type array, but you may want to work with strongly typed collections to avoid the type conversion. You can create those collections by hand or using a tool like CodeSmith [1].
HTH, Christoph Schittko MS MVP XML http://weblogs.asp.net/cschittko
[0] http://www.microsoft.com/downloads/d...A1C93FA-7462-4 7D0-8E56-8DD34C6292F0&displaylang=en [1] http://www.ericjsmith.net/codesmith/
-----Original Message----- From: Ice [mailto:ic*@nospam.com] Posted At: Thursday, November 25, 2004 10:08 PM Posted To: microsoft.public.dotnet.xml Conversation: xml serialization Subject: xml serialization
All -
I'm pretty comfortable with simple XML serialization of objects. However I observed something the other day and I wanted to know if I solved it the right way.
Basically if I have a string which contains a single xml node, I can serialize with this line of code:
object = (object)serializer.Deserialize(new XmlTextReader(xml,XmlNodeType.Element,null));
where xml is the string that represents the node
However if the string contains two nodes that individually would serialize to the "object" I have to do the following to be able to serialize to an array. Code looks like this:
string xml = "<ArrayOfObject >" + xml + "</ArrayOfObject>"; object = (object[])serializer.Deserialize(new XmlTextReader(xml,XmlNodeType.Element,null));
If don't concatenate "ArrayOfObject" elements to beginning and end of nodes, it doesn work. I get the exception "...xmlns = ''" wasn't expected...". Have I solved this problem correctly, or is there another way to get around this. ice
I haven't used it that much personally, but I've worked on a few
projects where it was used quite a bit.
Christoph Schittko
MVP XML http://weblogs.asp.net/cschittko -----Original Message----- From: Ice [mailto:ic*@nospam.com] Posted At: Sunday, November 28, 2004 10:02 AM Posted To: microsoft.public.dotnet.xml Conversation: xml serialization Subject: Re: xml serialization
thanks for the reply. your solution also works well. have you used CodeSmith extensively?
ice "Christoph Schittko [MVP]" <IN**********@austin.rr.com> wrote in
message news:Os**************@TK2MSFTNGP09.phx.gbl... Ice,
As you've seen, the XmlSerializer cannot deserialize XML Document fragments with Arrays correctly -- and glancing through the Rotor
source [0], I couldn't find anything that would generate code that would be what you are looking for.
Another solution avoiding the string concatenation would be to deserialize one array item at a time like in the example below:
string xml =
"<MyClass><foo>foobar</foo></MyClass><MyClass><foo>bar</foo></MyClass>"; XmlSerializer ser = new XmlSerializer(typeof(MyClass));
XmlTextReader reader = new XmlTextReader(xml, XmlNodeType.Element, null); object o; ArrayList list = new ArrayList(); MyClass[] objs;
while( false == reader.EOF ) { o = ser.Deserialize(reader); list.Add(o); } objs = list.ToArray(typeof(MyClass)) as MyClass[];
In the example, I store all the deserialized items in an ArrayList first, to later convert that to a type array, but you may want to
work with strongly typed collections to avoid the type conversion. You
can create those collections by hand or using a tool like CodeSmith [1].
HTH, Christoph Schittko MS MVP XML http://weblogs.asp.net/cschittko
[0] http://www.microsoft.com/downloads/d...A1C93FA-7462-4 7D0-8E56-8DD34C6292F0&displaylang=en [1] http://www.ericjsmith.net/codesmith/
-----Original Message----- From: Ice [mailto:ic*@nospam.com] Posted At: Thursday, November 25, 2004 10:08 PM Posted To: microsoft.public.dotnet.xml Conversation: xml serialization Subject: xml serialization
All -
I'm pretty comfortable with simple XML serialization of objects. However I observed something the other day and I wanted to know if I solved
it the right way.
Basically if I have a string which contains a single xml node, I
can serialize with this line of code:
object = (object)serializer.Deserialize(new XmlTextReader(xml,XmlNodeType.Element,null));
where xml is the string that represents the node
However if the string contains two nodes that individually would serialize to the "object" I have to do the following to be able to serialize
to an array. Code looks like this:
string xml = "<ArrayOfObject >" + xml + "</ArrayOfObject>"; object = (object[])serializer.Deserialize(new XmlTextReader(xml,XmlNodeType.Element,null));
If don't concatenate "ArrayOfObject" elements to beginning and end
of nodes, it doesn work. I get the exception "...xmlns = ''" wasn't expected...". Have I solved this problem correctly, or is there another way to
get around this. ice
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ben |
last post by:
Hi, there.
Recently I was working on a problem where we want to save generic
closures in a data structure (a vector). The closure should work for
any data type and any method with pre-defined...
|
by: andrewcw |
last post by:
There is an error in XML document (1, 2).
I used XML spy to create the XML and XSD. When I asked to
have the XML validated it said it was OK.
I used the .net SDK to generate the class. I have...
|
by: Aaron Clamage |
last post by:
Hi,
I'm not sure that if this is the right forum, but any help would be greatly
appreciated.
I am porting some java serialization code to c# and I can't figure out the
correct way to do it. ...
|
by: Uttam |
last post by:
Hello,
We are at a very crucial decision making stage to select between .Net
and Java. Our requirement is to download a class at runtime on the
client computer and execute it using remoting or...
|
by: Alexander |
last post by:
When i store rule on PC with .NET.SP1 i cant restore them from PC without
SP1.
An i get this Error:
System.Runtime.Serialization.SerializationException:
Possible Version mismatch. Type...
|
by: mijalko |
last post by:
Hi,
I have inherited my class from System.Drawing.Printing.PrintDocument
and I wish to serialize this object using XmlSerializer. And I get
exception "There was an error reflecting type ...". If I...
|
by: Nikola Skoric |
last post by:
I ran in Mono a program developed on .NET Framework 2.0 and it ran OK
until I tried to desirialize a object. There the program died abruptly
dumping this:
System.ArgumentOutOfRangeException:...
|
by: bharathreddy |
last post by:
Before going to that i want to say few thing on serialization :
Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an...
|
by: kikisan |
last post by:
I am developing a windows service which utilizes the following classes:
interface IPersistable;
abstract class PersistableObject : IPersistable;
|
by: mkvenkit.vc |
last post by:
Hello,
I hope this is the right place to post a question on Boost. If not,
please let me know where I can post this message and I will do so.
I am having a strange problem with std::string as...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
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...
| |