Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / November 2004

Tip: Looking for answers? Try searching our database.

xml serialization

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ice - 26 Nov 2004 04:06 GMT
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
Christoph Schittko [MVP] - 26 Nov 2004 20:57 GMT
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/details.aspx?FamilyId=3A1C93FA-7462-4
7D0-8E56-8DD34C6292F0&displaylang=en
[1] http://www.ericjsmith.net/codesmith/

> -----Original Message-----
> From: Ice [mailto:ice@nospam.com]
[quoted text clipped - 34 lines]
> this.
> ice
Ice - 28 Nov 2004 16:01 GMT
thanks for the reply. your solution also works well.  have you used
CodeSmith extensively?

ice
> Ice,
>
[quoted text clipped - 81 lines]
> > this.
> > ice
Christoph Schittko [MVP] - 28 Nov 2004 21:06 GMT
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:ice@nospam.com]
[quoted text clipped - 18 lines]
> >
> > string xml =

"<MyClass><foo>foobar</foo></MyClass><MyClass><foo>bar</foo></MyClass>";
> > XmlSerializer ser = new XmlSerializer(typeof(MyClass));
> >
[quoted text clipped - 22 lines]
> >
> > [0]

http://www.microsoft.com/downloads/details.aspx?FamilyId=3A1C93FA-7462-4
> > 7D0-8E56-8DD34C6292F0&displaylang=en
> > [1] http://www.ericjsmith.net/codesmith/
[quoted text clipped - 42 lines]
> > > this.
> > > ice

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.