473,418 Members | 2,052 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,418 software developers and data experts.

serialize 2 objects to same xml file?

Can I add (append) to an xml file that already contains a
serialized object, and be able to deserialize to either
or both objects from the same file...??? How is this
done...??

thanks,

vince
Nov 15 '05 #1
14 14253
Chad,

Thanks for the info, tried your method and got both
objects to serialize to the same file, and the xml looks
well-formed.

However, when attempting to deserialize the first object
in the file, I get an error, "There is an error in xml
document(7,1)"... Line 7 is the beginning of the second
serialized object.

In looking at a previous version of an xml file that
contained only the first object, the xml is identical for
that object in the new 2 object xml file...

It would seem that the Deserialize() method is choking on
the fact that there is more than one object to
deserialize...

Is there a step that I'm forgetting somewhere...??

thanks for any help...

vince
-----Original Message-----

"vince" <vl******@sdcera.org> wrote in message
news:00****************************@phx.gbl...
Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either
or both objects from the same file...??? How is this
done...??


Create an XmlTextWriter (we'll call him xw)
Create Two XmlSerializers initialized with each type
and instance you wish to serialize. (we'll call them
1 and 2)

1.Serialize(xw);
2.Serialize(xw);

The only catch is, you have to deserialize them in
the same order, or remember how long 1 is. If you
wish to deserialize two, you have to create an
XmlTextReader and advance to the position in the
XML where 2 starts and then call:

2.Deserialize(xr);

-c
.

Nov 15 '05 #2
Actually, if you serialize 2 objects to XML, you'll
have two top-level tags which would be invalid XML.
I just thought of that, sorry.

You may have to have a root XML node and add the
serialized nodes underneath that.

What you may do is have something like:

<root>
<object num="1">
(serialization info here)
</object>
<object num="2">
(serialization info here)
</object>
</root>

Open the XML in an XmlDocument and do:

XmlNodeList objectNodes = doc.SelectNodes("//object");

Foo[] foos = new Foo[objectNodes.Count];

int fooCtr = 0;

foreach( XmlNode objectNode in objectNodes )
{
XmlSerializer ser = new XmlSerializer(typeof(Foo));

foos[fooCtr++] = (Foo) ser.Deserialize(
new StringReader(objectNode.InnerXml));
}

-c
"vince" <vl******@sdcera.org> wrote in message
news:09****************************@phx.gbl...
Chad,

Thanks for the info, tried your method and got both
objects to serialize to the same file, and the xml looks
well-formed.

However, when attempting to deserialize the first object
in the file, I get an error, "There is an error in xml
document(7,1)"... Line 7 is the beginning of the second
serialized object.

In looking at a previous version of an xml file that
contained only the first object, the xml is identical for
that object in the new 2 object xml file...

It would seem that the Deserialize() method is choking on
the fact that there is more than one object to
deserialize...

Is there a step that I'm forgetting somewhere...??

thanks for any help...

vince
-----Original Message-----

"vince" <vl******@sdcera.org> wrote in message
news:00****************************@phx.gbl...
Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either
or both objects from the same file...??? How is this
done...??


Create an XmlTextWriter (we'll call him xw)
Create Two XmlSerializers initialized with each type
and instance you wish to serialize. (we'll call them
1 and 2)

1.Serialize(xw);
2.Serialize(xw);

The only catch is, you have to deserialize them in
the same order, or remember how long 1 is. If you
wish to deserialize two, you have to create an
XmlTextReader and advance to the position in the
XML where 2 starts and then call:

2.Deserialize(xr);

-c
.

Nov 15 '05 #3
Chad,

thanks again... so, to clarify, the workaround for the
problem of having two top level tags is to manually parse
the xml file that was created...???

Sorta new to xml so I'll have to take your code snippet
and study it for a bit...

Looks like you load an intermediate array with the two
chunks of object data that you've parsed outa the
original 2 object xml file... is this correct?

thanks for the help,

vince
-----Original Message-----
Actually, if you serialize 2 objects to XML, you'll
have two top-level tags which would be invalid XML.
I just thought of that, sorry.

You may have to have a root XML node and add the
serialized nodes underneath that.

What you may do is have something like:

<root>
<object num="1">
(serialization info here)
</object>
<object num="2">
(serialization info here)
</object>
</root>

Open the XML in an XmlDocument and do:

XmlNodeList objectNodes = doc.SelectNodes("//object");

Foo[] foos = new Foo[objectNodes.Count];

int fooCtr = 0;

foreach( XmlNode objectNode in objectNodes )
{
XmlSerializer ser = new XmlSerializer(typeof(Foo));

foos[fooCtr++] = (Foo) ser.Deserialize(
new StringReader(objectNode.InnerXml));
}

-c
"vince" <vl******@sdcera.org> wrote in message
news:09****************************@phx.gbl...
Chad,

Thanks for the info, tried your method and got both
objects to serialize to the same file, and the xml looks well-formed.

However, when attempting to deserialize the first object in the file, I get an error, "There is an error in xml
document(7,1)"... Line 7 is the beginning of the second
serialized object.

In looking at a previous version of an xml file that
contained only the first object, the xml is identical for that object in the new 2 object xml file...

It would seem that the Deserialize() method is choking on the fact that there is more than one object to
deserialize...

Is there a step that I'm forgetting somewhere...??

thanks for any help...

vince
>-----Original Message-----
>
>"vince" <vl******@sdcera.org> wrote in message
>news:00****************************@phx.gbl...
>> Can I add (append) to an xml file that already

contains a
>> serialized object, and be able to deserialize to either >> or both objects from the same file...??? How is this
>> done...??
>
>Create an XmlTextWriter (we'll call him xw)
>Create Two XmlSerializers initialized with each type
>and instance you wish to serialize. (we'll call them
>1 and 2)
>
>1.Serialize(xw);
>2.Serialize(xw);
>
>The only catch is, you have to deserialize them in
>the same order, or remember how long 1 is. If you
>wish to deserialize two, you have to create an
>XmlTextReader and advance to the position in the
>XML where 2 starts and then call:
>
>2.Deserialize(xr);
>
>-c
>
>
>.
>

.

Nov 15 '05 #4

"vince" <vl******@sdcera.org> wrote in message
news:03****************************@phx.gbl...
Chad,

thanks again... so, to clarify, the workaround for the
problem of having two top level tags is to manually parse
the xml file that was created...???
Kinda, I guess. XML must have ONE root tag.

You can have multiple tags underneath this, but the problem,
I think, is that the XmlSerializer won't understand this
extra tag. It expects a single tag with all the object
contents underneath.

So, if you wish to have multiple objects in a single
XML, you must accomodate the XmlSerializer.

To do this, I recommend adding another tag called
<object> or something and you can add your own meta-data
and attributes to this in the future (like the version of
the object contained herein, etc).

You must take the contents of that tag and pass it to
the XmlSerializer to deserialize.
Sorta new to xml so I'll have to take your code snippet
and study it for a bit...

Looks like you load an intermediate array with the two
chunks of object data that you've parsed outa the
original 2 object xml file... is this correct?


Since you have multiple objects in the XML to deserialize,
you have to store the newly reconstituted objects somewhere.
You can use an ArrayList, Hashtable, or most any Collection
for that matter.

I figured you wouldn't have that many objects and you
already know how many there are, so it's probably
most efficient just to create an array and add them as
you deserialize them from the XML

-c

-----Original Message-----
Actually, if you serialize 2 objects to XML, you'll
have two top-level tags which would be invalid XML.
I just thought of that, sorry.

You may have to have a root XML node and add the
serialized nodes underneath that.

What you may do is have something like:

<root>
<object num="1">
(serialization info here)
</object>
<object num="2">
(serialization info here)
</object>
</root>

Open the XML in an XmlDocument and do:

XmlNodeList objectNodes = doc.SelectNodes("//object");

Foo[] foos = new Foo[objectNodes.Count];

int fooCtr = 0;

foreach( XmlNode objectNode in objectNodes )
{
XmlSerializer ser = new XmlSerializer(typeof(Foo));

foos[fooCtr++] = (Foo) ser.Deserialize(
new StringReader(objectNode.InnerXml));
}

-c
"vince" <vl******@sdcera.org> wrote in message
news:09****************************@phx.gbl...
Chad,

Thanks for the info, tried your method and got both
objects to serialize to the same file, and the xml looks well-formed.

However, when attempting to deserialize the first object in the file, I get an error, "There is an error in xml
document(7,1)"... Line 7 is the beginning of the second
serialized object.

In looking at a previous version of an xml file that
contained only the first object, the xml is identical for that object in the new 2 object xml file...

It would seem that the Deserialize() method is choking on the fact that there is more than one object to
deserialize...

Is there a step that I'm forgetting somewhere...??

thanks for any help...

vince

>-----Original Message-----
>
>"vince" <vl******@sdcera.org> wrote in message
>news:00****************************@phx.gbl...
>> Can I add (append) to an xml file that already
contains a
>> serialized object, and be able to deserialize to either >> or both objects from the same file...??? How is this
>> done...??
>
>Create an XmlTextWriter (we'll call him xw)
>Create Two XmlSerializers initialized with each type
>and instance you wish to serialize. (we'll call them
>1 and 2)
>
>1.Serialize(xw);
>2.Serialize(xw);
>
>The only catch is, you have to deserialize them in
>the same order, or remember how long 1 is. If you
>wish to deserialize two, you have to create an
>XmlTextReader and advance to the position in the
>XML where 2 starts and then call:
>
>2.Deserialize(xr);
>
>-c
>
>
>.
>

.

Nov 15 '05 #5
Chad,

so to "do it right" and have one root tag, I'd want to
create a serializable wrapper object that contained an
instance of each serializable object (2 in this case),
then serialize the wrapper object, correct?

I assume this would allow both contained objects to be
deserialized in the normal fashion...???

thanks for your time...

vince
-----Original Message-----

"vince" <vl******@sdcera.org> wrote in message
news:03****************************@phx.gbl...
Chad,

thanks again... so, to clarify, the workaround for the
problem of having two top level tags is to manually parse
the xml file that was created...???
Kinda, I guess. XML must have ONE root tag.

You can have multiple tags underneath this, but the

problem,I think, is that the XmlSerializer won't understand this
extra tag. It expects a single tag with all the object
contents underneath.

So, if you wish to have multiple objects in a single
XML, you must accomodate the XmlSerializer.

To do this, I recommend adding another tag called
<object> or something and you can add your own meta-data
and attributes to this in the future (like the version of
the object contained herein, etc).

You must take the contents of that tag and pass it to
the XmlSerializer to deserialize.
Sorta new to xml so I'll have to take your code snippet
and study it for a bit...

Looks like you load an intermediate array with the two
chunks of object data that you've parsed outa the
original 2 object xml file... is this correct?
Since you have multiple objects in the XML to

deserialize,you have to store the newly reconstituted objects somewhere.You can use an ArrayList, Hashtable, or most any Collectionfor that matter.

I figured you wouldn't have that many objects and you
already know how many there are, so it's probably
most efficient just to create an array and add them as
you deserialize them from the XML

-c

>-----Original Message-----
>Actually, if you serialize 2 objects to XML, you'll
>have two top-level tags which would be invalid XML.
>I just thought of that, sorry.
>
>You may have to have a root XML node and add the
>serialized nodes underneath that.
>
>What you may do is have something like:
>
><root>
> <object num="1">
> (serialization info here)
> </object>
> <object num="2">
> (serialization info here)
> </object>
></root>
>
>Open the XML in an XmlDocument and do:
>
>XmlNodeList objectNodes = doc.SelectNodes("//object");
>
>Foo[] foos = new Foo[objectNodes.Count];
>
>int fooCtr = 0;
>
>foreach( XmlNode objectNode in objectNodes )
>{
> XmlSerializer ser = new XmlSerializer(typeof (Foo)); >
> foos[fooCtr++] = (Foo) ser.Deserialize(
> new StringReader(objectNode.InnerXml));
>}
>
>-c
>
>
>"vince" <vl******@sdcera.org> wrote in message
>news:09****************************@phx.gbl...
>> Chad,
>>
>> Thanks for the info, tried your method and got both
>> objects to serialize to the same file, and the xml

looks
>> well-formed.
>>
>> However, when attempting to deserialize the first

object
>> in the file, I get an error, "There is an error in xml >> document(7,1)"... Line 7 is the beginning of the second >> serialized object.
>>
>> In looking at a previous version of an xml file that
>> contained only the first object, the xml is identical
for
>> that object in the new 2 object xml file...
>>
>> It would seem that the Deserialize() method is
choking on
>> the fact that there is more than one object to
>> deserialize...
>>
>> Is there a step that I'm forgetting somewhere...??
>>
>> thanks for any help...
>>
>> vince
>>
>> >-----Original Message-----
>> >
>> >"vince" <vl******@sdcera.org> wrote in message
>> >news:00****************************@phx.gbl...
>> >> Can I add (append) to an xml file that already
>> contains a
>> >> serialized object, and be able to deserialize to

either
>> >> or both objects from the same file...??? How is

this >> >> done...??
>> >
>> >Create an XmlTextWriter (we'll call him xw)
>> >Create Two XmlSerializers initialized with each type >> >and instance you wish to serialize. (we'll call them >> >1 and 2)
>> >
>> >1.Serialize(xw);
>> >2.Serialize(xw);
>> >
>> >The only catch is, you have to deserialize them in
>> >the same order, or remember how long 1 is. If you
>> >wish to deserialize two, you have to create an
>> >XmlTextReader and advance to the position in the
>> >XML where 2 starts and then call:
>> >
>> >2.Deserialize(xr);
>> >
>> >-c
>> >
>> >
>> >.
>> >
>
>
>.
>

.

Nov 15 '05 #6

"vince" <vl******@sdcera.org> wrote in message
news:0a****************************@phx.gbl...
Chad,

so to "do it right" and have one root tag, I'd want to
create a serializable wrapper object that contained an
instance of each serializable object (2 in this case),
then serialize the wrapper object, correct?

I assume this would allow both contained objects to be
deserialized in the normal fashion...???
That's not what I was saying, but that's an interesting
point. That might actually work. The only problem is,
if you wanted to change the number of subobjects, it
might be harder. You might just have a public propery
which is an array of the type of object you wish
to serialize.

As long as there's only one object, you don't need
the root tag (remember, you only need one top-level
tag, and if all you have is one object, then you
already have that).

I'm not sure which is the better approach. I haven't
had the chance to do a lot of serialization work
yet, so please try one or the other and let me know
how it goes.

-c
-----Original Message-----

"vince" <vl******@sdcera.org> wrote in message
news:03****************************@phx.gbl...
Chad,

thanks again... so, to clarify, the workaround for the
problem of having two top level tags is to manually parse the xml file that was created...???


Kinda, I guess. XML must have ONE root tag.

You can have multiple tags underneath this, but the

problem,
I think, is that the XmlSerializer won't understand this
extra tag. It expects a single tag with all the object
contents underneath.

So, if you wish to have multiple objects in a single
XML, you must accomodate the XmlSerializer.

To do this, I recommend adding another tag called
<object> or something and you can add your own meta-data
and attributes to this in the future (like the version of
the object contained herein, etc).

You must take the contents of that tag and pass it to
the XmlSerializer to deserialize.
Sorta new to xml so I'll have to take your code snippet
and study it for a bit...

Looks like you load an intermediate array with the two
chunks of object data that you've parsed outa the
original 2 object xml file... is this correct?


Since you have multiple objects in the XML to

deserialize,
you have to store the newly reconstituted objects

somewhere.
You can use an ArrayList, Hashtable, or most any

Collection
for that matter.

I figured you wouldn't have that many objects and you
already know how many there are, so it's probably
most efficient just to create an array and add them as
you deserialize them from the XML

-c

>-----Original Message-----
>Actually, if you serialize 2 objects to XML, you'll
>have two top-level tags which would be invalid XML.
>I just thought of that, sorry.
>
>You may have to have a root XML node and add the
>serialized nodes underneath that.
>
>What you may do is have something like:
>
><root>
> <object num="1">
> (serialization info here)
> </object>
> <object num="2">
> (serialization info here)
> </object>
></root>
>
>Open the XML in an XmlDocument and do:
>
>XmlNodeList objectNodes = doc.SelectNodes("//object");
>
>Foo[] foos = new Foo[objectNodes.Count];
>
>int fooCtr = 0;
>
>foreach( XmlNode objectNode in objectNodes )
>{
> XmlSerializer ser = new XmlSerializer(typeof (Foo)); >
> foos[fooCtr++] = (Foo) ser.Deserialize(
> new StringReader(objectNode.InnerXml));
>}
>
>-c
>
>
>"vince" <vl******@sdcera.org> wrote in message
>news:09****************************@phx.gbl...
>> Chad,
>>
>> Thanks for the info, tried your method and got both
>> objects to serialize to the same file, and the xml
looks
>> well-formed.
>>
>> However, when attempting to deserialize the first
object
>> in the file, I get an error, "There is an error in xml >> document(7,1)"... Line 7 is the beginning of the second >> serialized object.
>>
>> In looking at a previous version of an xml file that
>> contained only the first object, the xml is identical for
>> that object in the new 2 object xml file...
>>
>> It would seem that the Deserialize() method is choking on
>> the fact that there is more than one object to
>> deserialize...
>>
>> Is there a step that I'm forgetting somewhere...??
>>
>> thanks for any help...
>>
>> vince
>>
>> >-----Original Message-----
>> >
>> >"vince" <vl******@sdcera.org> wrote in message
>> >news:00****************************@phx.gbl...
>> >> Can I add (append) to an xml file that already
>> contains a
>> >> serialized object, and be able to deserialize to
either
>> >> or both objects from the same file...??? How is this >> >> done...??
>> >
>> >Create an XmlTextWriter (we'll call him xw)
>> >Create Two XmlSerializers initialized with each type >> >and instance you wish to serialize. (we'll call them >> >1 and 2)
>> >
>> >1.Serialize(xw);
>> >2.Serialize(xw);
>> >
>> >The only catch is, you have to deserialize them in
>> >the same order, or remember how long 1 is. If you
>> >wish to deserialize two, you have to create an
>> >XmlTextReader and advance to the position in the
>> >XML where 2 starts and then call:
>> >
>> >2.Deserialize(xr);
>> >
>> >-c
>> >
>> >
>> >.
>> >
>
>
>.
>

.

Nov 15 '05 #7
Chad,

tried your method using an xmldoc, was able to do a
SelectNodes() on the first serialized object, but got
back a count of 0 when attempting to do the same thing
with the second serialized object...

I guess the extra root tag for the second object has got
me hosed everywhere, looks like I'll hafta look into
creating a wrapper for both objects...

thanks for your time...

vince
-----Original Message-----

"vince" <vl******@sdcera.org> wrote in message
news:0a****************************@phx.gbl...
Chad,

so to "do it right" and have one root tag, I'd want to
create a serializable wrapper object that contained an
instance of each serializable object (2 in this case),
then serialize the wrapper object, correct?

I assume this would allow both contained objects to be
deserialized in the normal fashion...???


That's not what I was saying, but that's an interesting
point. That might actually work. The only problem is,
if you wanted to change the number of subobjects, it
might be harder. You might just have a public propery
which is an array of the type of object you wish
to serialize.

As long as there's only one object, you don't need
the root tag (remember, you only need one top-level
tag, and if all you have is one object, then you
already have that).

I'm not sure which is the better approach. I haven't
had the chance to do a lot of serialization work
yet, so please try one or the other and let me know
how it goes.

-c
>-----Original Message-----
>
>"vince" <vl******@sdcera.org> wrote in message
>news:03****************************@phx.gbl...
>> Chad,
>>
>> thanks again... so, to clarify, the workaround for the >> problem of having two top level tags is to manually

parse
>> the xml file that was created...???
>
>Kinda, I guess. XML must have ONE root tag.
>
>You can have multiple tags underneath this, but the

problem,
>I think, is that the XmlSerializer won't understand this >extra tag. It expects a single tag with all the object
>contents underneath.
>
>So, if you wish to have multiple objects in a single
>XML, you must accomodate the XmlSerializer.
>
>To do this, I recommend adding another tag called
><object> or something and you can add your own meta- data >and attributes to this in the future (like the version of >the object contained herein, etc).
>
>You must take the contents of that tag and pass it to
>the XmlSerializer to deserialize.
>
>> Sorta new to xml so I'll have to take your code snippet >> and study it for a bit...
>>
>> Looks like you load an intermediate array with the two >> chunks of object data that you've parsed outa the
>> original 2 object xml file... is this correct?
>
>Since you have multiple objects in the XML to

deserialize,
>you have to store the newly reconstituted objects

somewhere.
>You can use an ArrayList, Hashtable, or most any

Collection
>for that matter.
>
>I figured you wouldn't have that many objects and you
>already know how many there are, so it's probably
>most efficient just to create an array and add them as
>you deserialize them from the XML
>
>-c
>
>
>> >-----Original Message-----
>> >Actually, if you serialize 2 objects to XML, you'll
>> >have two top-level tags which would be invalid XML.
>> >I just thought of that, sorry.
>> >
>> >You may have to have a root XML node and add the
>> >serialized nodes underneath that.
>> >
>> >What you may do is have something like:
>> >
>> ><root>
>> > <object num="1">
>> > (serialization info here)
>> > </object>
>> > <object num="2">
>> > (serialization info here)
>> > </object>
>> ></root>
>> >
>> >Open the XML in an XmlDocument and do:
>> >
>> >XmlNodeList objectNodes = doc.SelectNodes ("//object"); >> >
>> >Foo[] foos = new Foo[objectNodes.Count];
>> >
>> >int fooCtr = 0;
>> >
>> >foreach( XmlNode objectNode in objectNodes )
>> >{
>> > XmlSerializer ser = new XmlSerializer(typeof

(Foo));
>> >
>> > foos[fooCtr++] = (Foo) ser.Deserialize(
>> > new StringReader(objectNode.InnerXml));
>> >}
>> >
>> >-c
>> >
>> >
>> >"vince" <vl******@sdcera.org> wrote in message
>> >news:09****************************@phx.gbl...
>> >> Chad,
>> >>
>> >> Thanks for the info, tried your method and got both >> >> objects to serialize to the same file, and the xml >> looks
>> >> well-formed.
>> >>
>> >> However, when attempting to deserialize the first
>> object
>> >> in the file, I get an error, "There is an error in
xml
>> >> document(7,1)"... Line 7 is the beginning of the

second
>> >> serialized object.
>> >>
>> >> In looking at a previous version of an xml file
that >> >> contained only the first object, the xml is

identical
>> for
>> >> that object in the new 2 object xml file...
>> >>
>> >> It would seem that the Deserialize() method is

choking
>> on
>> >> the fact that there is more than one object to
>> >> deserialize...
>> >>
>> >> Is there a step that I'm forgetting somewhere...?? >> >>
>> >> thanks for any help...
>> >>
>> >> vince
>> >>
>> >> >-----Original Message-----
>> >> >
>> >> >"vince" <vl******@sdcera.org> wrote in message
>> >> >news:00****************************@phx.gbl...
>> >> >> Can I add (append) to an xml file that already
>> >> contains a
>> >> >> serialized object, and be able to deserialize to >> either
>> >> >> or both objects from the same file...??? How is this
>> >> >> done...??
>> >> >
>> >> >Create an XmlTextWriter (we'll call him xw)
>> >> >Create Two XmlSerializers initialized with each

type
>> >> >and instance you wish to serialize. (we'll call

them
>> >> >1 and 2)
>> >> >
>> >> >1.Serialize(xw);
>> >> >2.Serialize(xw);
>> >> >
>> >> >The only catch is, you have to deserialize them

in >> >> >the same order, or remember how long 1 is. If you >> >> >wish to deserialize two, you have to create an
>> >> >XmlTextReader and advance to the position in the
>> >> >XML where 2 starts and then call:
>> >> >
>> >> >2.Deserialize(xr);
>> >> >
>> >> >-c
>> >> >
>> >> >
>> >> >.
>> >> >
>> >
>> >
>> >.
>> >
>
>
>.
>

.

Nov 15 '05 #8
SelectNodes() takes an XPath query. This is kinda like
the directory structure in DOS/UNIX.

If you had, for example:

<root>
<object>...</object>
<object>...</object>
<object>...</object>
</root>

And you did a query on the doc like:

doc.SelectNodes("/root/object") or
even "//object", you would get back an
XmlNodeList with 3 nodes in it.

If you wanted to, you can call SelectNodes()
and/or SelectSingleNode() on an XmlNode as
well. It's useful if you're navigating a
huge tree of XML.

XPath is pretty cool, if you get some time,
you should read up on it.

-c

"vince" <vl******@sdcera.org> wrote in message
news:0a****************************@phx.gbl...
Chad,

tried your method using an xmldoc, was able to do a
SelectNodes() on the first serialized object, but got
back a count of 0 when attempting to do the same thing
with the second serialized object...

I guess the extra root tag for the second object has got
me hosed everywhere, looks like I'll hafta look into
creating a wrapper for both objects...

thanks for your time...

vince
-----Original Message-----

"vince" <vl******@sdcera.org> wrote in message
news:0a****************************@phx.gbl...
Chad,

so to "do it right" and have one root tag, I'd want to
create a serializable wrapper object that contained an
instance of each serializable object (2 in this case),
then serialize the wrapper object, correct?

I assume this would allow both contained objects to be
deserialized in the normal fashion...???


That's not what I was saying, but that's an interesting
point. That might actually work. The only problem is,
if you wanted to change the number of subobjects, it
might be harder. You might just have a public propery
which is an array of the type of object you wish
to serialize.

As long as there's only one object, you don't need
the root tag (remember, you only need one top-level
tag, and if all you have is one object, then you
already have that).

I'm not sure which is the better approach. I haven't
had the chance to do a lot of serialization work
yet, so please try one or the other and let me know
how it goes.

-c

>-----Original Message-----
>
>"vince" <vl******@sdcera.org> wrote in message
>news:03****************************@phx.gbl...
>> Chad,
>>
>> thanks again... so, to clarify, the workaround for the >> problem of having two top level tags is to manually
parse
>> the xml file that was created...???
>
>Kinda, I guess. XML must have ONE root tag.
>
>You can have multiple tags underneath this, but the
problem,
>I think, is that the XmlSerializer won't understand this >extra tag. It expects a single tag with all the object
>contents underneath.
>
>So, if you wish to have multiple objects in a single
>XML, you must accomodate the XmlSerializer.
>
>To do this, I recommend adding another tag called
><object> or something and you can add your own meta- data >and attributes to this in the future (like the version of >the object contained herein, etc).
>
>You must take the contents of that tag and pass it to
>the XmlSerializer to deserialize.
>
>> Sorta new to xml so I'll have to take your code snippet >> and study it for a bit...
>>
>> Looks like you load an intermediate array with the two >> chunks of object data that you've parsed outa the
>> original 2 object xml file... is this correct?
>
>Since you have multiple objects in the XML to
deserialize,
>you have to store the newly reconstituted objects
somewhere.
>You can use an ArrayList, Hashtable, or most any
Collection
>for that matter.
>
>I figured you wouldn't have that many objects and you
>already know how many there are, so it's probably
>most efficient just to create an array and add them as
>you deserialize them from the XML
>
>-c
>
>
>> >-----Original Message-----
>> >Actually, if you serialize 2 objects to XML, you'll
>> >have two top-level tags which would be invalid XML.
>> >I just thought of that, sorry.
>> >
>> >You may have to have a root XML node and add the
>> >serialized nodes underneath that.
>> >
>> >What you may do is have something like:
>> >
>> ><root>
>> > <object num="1">
>> > (serialization info here)
>> > </object>
>> > <object num="2">
>> > (serialization info here)
>> > </object>
>> ></root>
>> >
>> >Open the XML in an XmlDocument and do:
>> >
>> >XmlNodeList objectNodes = doc.SelectNodes ("//object"); >> >
>> >Foo[] foos = new Foo[objectNodes.Count];
>> >
>> >int fooCtr = 0;
>> >
>> >foreach( XmlNode objectNode in objectNodes )
>> >{
>> > XmlSerializer ser = new XmlSerializer(typeof
(Foo));
>> >
>> > foos[fooCtr++] = (Foo) ser.Deserialize(
>> > new StringReader(objectNode.InnerXml));
>> >}
>> >
>> >-c
>> >
>> >
>> >"vince" <vl******@sdcera.org> wrote in message
>> >news:09****************************@phx.gbl...
>> >> Chad,
>> >>
>> >> Thanks for the info, tried your method and got both >> >> objects to serialize to the same file, and the xml >> looks
>> >> well-formed.
>> >>
>> >> However, when attempting to deserialize the first
>> object
>> >> in the file, I get an error, "There is an error in xml
>> >> document(7,1)"... Line 7 is the beginning of the
second
>> >> serialized object.
>> >>
>> >> In looking at a previous version of an xml file that >> >> contained only the first object, the xml is
identical
>> for
>> >> that object in the new 2 object xml file...
>> >>
>> >> It would seem that the Deserialize() method is
choking
>> on
>> >> the fact that there is more than one object to
>> >> deserialize...
>> >>
>> >> Is there a step that I'm forgetting somewhere...?? >> >>
>> >> thanks for any help...
>> >>
>> >> vince
>> >>
>> >> >-----Original Message-----
>> >> >
>> >> >"vince" <vl******@sdcera.org> wrote in message
>> >> >news:00****************************@phx.gbl...
>> >> >> Can I add (append) to an xml file that already
>> >> contains a
>> >> >> serialized object, and be able to deserialize to >> either
>> >> >> or both objects from the same file...??? How is this
>> >> >> done...??
>> >> >
>> >> >Create an XmlTextWriter (we'll call him xw)
>> >> >Create Two XmlSerializers initialized with each
type
>> >> >and instance you wish to serialize. (we'll call
them
>> >> >1 and 2)
>> >> >
>> >> >1.Serialize(xw);
>> >> >2.Serialize(xw);
>> >> >
>> >> >The only catch is, you have to deserialize them in >> >> >the same order, or remember how long 1 is. If you >> >> >wish to deserialize two, you have to create an
>> >> >XmlTextReader and advance to the position in the
>> >> >XML where 2 starts and then call:
>> >> >
>> >> >2.Deserialize(xr);
>> >> >
>> >> >-c
>> >> >
>> >> >
>> >> >.
>> >> >
>> >
>> >
>> >.
>> >
>
>
>.
>

.

Nov 15 '05 #9
Vince,

I replied to your question in microsoft.public.dotnet.general. In the
future, please don't repost the same question to multiple groups.

There's no need to use strange workarounds to get this done. Just define an
array of objects (or ArrayList, or strongly-typed collection), place both
items in the array, and serialize/deserialize the array. It will work fine.

--Robert Jacobson

"vince" <vl******@sdcera.org> wrote in message
news:00****************************@phx.gbl...
Can I add (append) to an xml file that already contains a
serialized object, and be able to deserialize to either
or both objects from the same file...??? How is this
done...??

thanks,

vince

Nov 15 '05 #10
> .... in microsoft.public.dotnet.general.

Correction: microsoft.public.dotnet.vb
Nov 15 '05 #11
Robert,

tried your suggestion of putting the 2 serializable
objects into an Arraylist, and also an array of type
object[], but I get runtime errors... here's the code:

object[] jobArray = new object[2];

jobArray[0] = theJob; //serializable custom type w/
arraylist
jobArray[1] = myTask; //arraylist
XmlSerializer x2 = new XmlSerializer(typeof(object[]));
TextWriter tw = new StreamWriter("c:\\backup\\" +
textBox1.Text + ".xml");

x2.Serialize(tw, jobArray);

The exception thrown is "Job.JobFile may not be used in
this context".... Job is the namespace, and theJob is an
object of type Job.JobFile.

Did the same thing using an Arraylist to hold the two
objects with similar results...

Thanks for any further suggestions... sorry for the
double post on newsgroups..

-----Original Message-----
Vince,

I replied to your question in microsoft.public.dotnet.general. In thefuture, please don't repost the same question to multiple groups.
There's no need to use strange workarounds to get this done. Just define anarray of objects (or ArrayList, or strongly-typed collection), place bothitems in the array, and serialize/deserialize the array. It will work fine.
--Robert Jacobson

"vince" <vl******@sdcera.org> wrote in message
news:00****************************@phx.gbl...
Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either
or both objects from the same file...??? How is this
done...??

thanks,

vince

.

Nov 15 '05 #12
Robert,

I would also add that theJob seriailizes fine
individually, but I can't seem to get it to do so when
it's an element of a parent container...

thanks for your time...

vince
-----Original Message-----
Robert,

tried your suggestion of putting the 2 serializable
objects into an Arraylist, and also an array of type
object[], but I get runtime errors... here's the code:

object[] jobArray = new object[2];

jobArray[0] = theJob; //serializable custom type w/
arraylist
jobArray[1] = myTask; //arraylist
XmlSerializer x2 = new XmlSerializer(typeof(object[]));
TextWriter tw = new StreamWriter("c:\\backup\\" +
textBox1.Text + ".xml");

x2.Serialize(tw, jobArray);

The exception thrown is "Job.JobFile may not be used in
this context".... Job is the namespace, and theJob is an
object of type Job.JobFile.

Did the same thing using an Arraylist to hold the two
objects with similar results...

Thanks for any further suggestions... sorry for the
double post on newsgroups..

-----Original Message-----
Vince,

I replied to your question in

microsoft.public.dotnet.general. In the
future, please don't repost the same question to

multiple groups.

There's no need to use strange workarounds to get this

done. Just define an
array of objects (or ArrayList, or strongly-typed

collection), place both
items in the array, and serialize/deserialize the

array. It will work fine.

--Robert Jacobson

"vince" <vl******@sdcera.org> wrote in message
news:00****************************@phx.gbl...
Can I add (append) to an xml file that alreadycontains a serialized object, and be able to deserialize to either or both objects from the same file...??? How is this
done...??

thanks,

vince

.

.

Nov 15 '05 #13
You're right -- it turns out you can't just stuff an Object[] array with
custom classes and expect it to serailize correctly. The reason is that the
XmlSerializer is kind of dumb... it needs to know what type of object is
being stored in the array so that it can deserialize it correctly later on.
Sorry for leading you astray. <g>

The easiest way around this is to use a strongly-typed array or collection.
So, it should work if you replace the line "object[] jobArray = new
object[2];" with "jobFile[] jobArray = new jobFile[2];" I have some sample
code below. Alternatively, you could define a strongly-typed JobFile
collection by inheriting from CollectionBase.

Another alternative is to use the SOAP serializer or binary serializer
instead. Both are more robust than the XmlSerializer, and should be able to
serialize Object[] arrays correctly. The tradeoff is that the output from
those serializers are not as easily readable (for human eyes) than the clean
XML generated by the XmlSerailizer. Check out these articles:

http://msdn.microsoft.com/library/de...bjserializ.asp
http://msdn.microsoft.com/library/de...et09252001.asp

Hope this helps,
Robert Jacobson
using System;
using System.Xml.Serialization;
using System.IO;
using System.Diagnostics;

namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
SerializationTest();
}

static void SerializationTest()
{
Object[] jobArray = new Object[2];

DummyJob job1 = new DummyJob();
job1.Name = "First Job";
jobArray[0] = job1;

DummyJob job2 = new DummyJob();
job2.Name = "Second Job";
jobArray[1] = job2;

try
{

XmlSerializer serializer = new XmlSerializer(jobArray.GetType());
StreamWriter sw = new StreamWriter("Test.xml");

serializer.Serialize(sw, jobArray);
sw.Close();
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
}

public class DummyJob
{
public string Name;
}

}
"vince" <vl******@sdcera.org> wrote in message
news:02****************************@phx.gbl...
Robert,

I would also add that theJob seriailizes fine
individually, but I can't seem to get it to do so when
it's an element of a parent container...

thanks for your time...

vince
-----Original Message-----
Robert,

tried your suggestion of putting the 2 serializable
objects into an Arraylist, and also an array of type
object[], but I get runtime errors... here's the code:

object[] jobArray = new object[2];

jobArray[0] = theJob; //serializable custom type w/
arraylist
jobArray[1] = myTask; //arraylist
XmlSerializer x2 = new XmlSerializer(typeof(object[]));
TextWriter tw = new StreamWriter("c:\\backup\\" +
textBox1.Text + ".xml");

x2.Serialize(tw, jobArray);

The exception thrown is "Job.JobFile may not be used in
this context".... Job is the namespace, and theJob is an
object of type Job.JobFile.

Did the same thing using an Arraylist to hold the two
objects with similar results...

Thanks for any further suggestions... sorry for the
double post on newsgroups..

-----Original Message-----
Vince,

I replied to your question in

microsoft.public.dotnet.general. In the
future, please don't repost the same question to

multiple groups.

There's no need to use strange workarounds to get this

done. Just define an
array of objects (or ArrayList, or strongly-typed

collection), place both
items in the array, and serialize/deserialize the

array. It will work fine.

--Robert Jacobson

"vince" <vl******@sdcera.org> wrote in message
news:00****************************@phx.gbl...
Can I add (append) to an xml file that already

contains a
serialized object, and be able to deserialize to either or both objects from the same file...??? How is this
done...??

thanks,

vince
.

.

Nov 15 '05 #14
Robert,

thanks for your help, it seems that my custom JobFile
type can't be serialized as a member of a strongly typed
array... tried several ways of doing this, but Serialize()
keeps telliing me that my JobFile type "can't be used in
this context".

I suspect that it's because the JobFile class inherits
from ICollection and is set up to serialize a contained
arraylist of custom objects. I think I'm trying to do too
much here, will look at your other suggested methods.

Here's the code I was using:

JobFile[] jobArray = new JobFile[2];

jobArray[0] = theJob;
jobArray[1] = theJob1;

XmlSerializer x = new XmlSerializer(jobArray.GetType());
TextWriter writer = new StreamWriter("c:\\backup\\" +
textBox1.Text + ".xml");
x.Serialize(writer, jobArray);

Thanks for your help,

Vince Lusardi
-----Original Message-----
You're right -- it turns out you can't just stuff an Object[] array withcustom classes and expect it to serailize correctly. The reason is that theXmlSerializer is kind of dumb... it needs to know what type of object isbeing stored in the array so that it can deserialize it correctly later on.Sorry for leading you astray. <g>

The easiest way around this is to use a strongly-typed array or collection.So, it should work if you replace the line "object[] jobArray = newobject[2];" with "jobFile[] jobArray = new jobFile[2];" I have some samplecode below. Alternatively, you could define a strongly- typed JobFilecollection by inheriting from CollectionBase.

Another alternative is to use the SOAP serializer or binary serializerinstead. Both are more robust than the XmlSerializer, and should be able toserialize Object[] arrays correctly. The tradeoff is that the output fromthose serializers are not as easily readable (for human eyes) than the cleanXML generated by the XmlSerailizer. Check out these articles:
http://msdn.microsoft.com/library/default.asp? url=/library/en-us/dndotnet/html/objserializ.asphttp://msdn.microsoft.com/library/default.asp? url=/library/en-us/dnadvnet/html/vbnet09252001.asp
Hope this helps,
Robert Jacobson
using System;
using System.Xml.Serialization;
using System.IO;
using System.Diagnostics;

namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
SerializationTest();
}

static void SerializationTest()
{
Object[] jobArray = new Object[2];

DummyJob job1 = new DummyJob();
job1.Name = "First Job";
jobArray[0] = job1;

DummyJob job2 = new DummyJob();
job2.Name = "Second Job";
jobArray[1] = job2;

try
{

XmlSerializer serializer = new XmlSerializer (jobArray.GetType()); StreamWriter sw = new StreamWriter("Test.xml");

serializer.Serialize(sw, jobArray);
sw.Close();
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
}

public class DummyJob
{
public string Name;
}

}
"vince" <vl******@sdcera.org> wrote in message
news:02****************************@phx.gbl...
Robert,

I would also add that theJob seriailizes fine
individually, but I can't seem to get it to do so when
it's an element of a parent container...

thanks for your time...

vince
>-----Original Message-----
>Robert,
>
>tried your suggestion of putting the 2 serializable
>objects into an Arraylist, and also an array of type
>object[], but I get runtime errors... here's the code:
>
>object[] jobArray = new object[2];
>
>jobArray[0] = theJob; //serializable custom type w/
>arraylist
>jobArray[1] = myTask; //arraylist
>XmlSerializer x2 = new XmlSerializer(typeof(object [])); >TextWriter tw = new StreamWriter("c:\\backup\\" +
>textBox1.Text + ".xml");
>
>x2.Serialize(tw, jobArray);
>
>The exception thrown is "Job.JobFile may not be used in >this context".... Job is the namespace, and theJob is an >object of type Job.JobFile.
>
>Did the same thing using an Arraylist to hold the two
>objects with similar results...
>
>Thanks for any further suggestions... sorry for the
>double post on newsgroups..
>
>
>>-----Original Message-----
>>Vince,
>>
>>I replied to your question in
>microsoft.public.dotnet.general. In the
>>future, please don't repost the same question to
>multiple groups.
>>
>>There's no need to use strange workarounds to get this >done. Just define an
>>array of objects (or ArrayList, or strongly-typed
>collection), place both
>>items in the array, and serialize/deserialize the
>array. It will work fine.
>>
>>--Robert Jacobson
>>
>>
>>
>>
>>
>>"vince" <vl******@sdcera.org> wrote in message
>>news:00****************************@phx.gbl...
>>> Can I add (append) to an xml file that already
>contains a
>>> serialized object, and be able to deserialize to

either
>>> or both objects from the same file...??? How is this >>> done...??
>>>
>>> thanks,
>>>
>>> vince
>>
>>
>>.
>>
>.
>

.

Nov 15 '05 #15

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

Similar topics

4
by: Julian Yap | last post by:
Hi all, I'm trying to get some ideas on the best way to do this. In this particular coding snippet, I was thinking of creating a dictionary of file objects and file names. These would be...
0
by: Tu-Thach | last post by:
I would not recommend doing this. Instead, create a wrapper object that holds the two serializable object and mark that wrapper object as serializable as well. Then, serialize that wrapper...
6
by: Affan Syed | last post by:
Hi, I am getting this weird problem. I know what i am doing is strange.. i am using C++ vectors and fopen, but for some reason if i used ofstream in the similar scenario it would give me errors....
0
by: Monorom | last post by:
Hi, Is it possible to serialize objects in XML without the declaration <?xml version="1.0"?> in the the result of the serialization. I want to insert this result into antoher XML stream. ...
0
by: Newbie H | last post by:
I can serialize several objects to the same file using binary formatter, yet only one using XML formatter I call the Serialize() method once per object, the objects are not contained in a collection...
3
by: Sharon | last post by:
I'm using StreamWriter to write to file and at the same time I'm using the StreamReader to read from the same file. Doing that cause an exception when I'm trying to create the StreamReader when...
4
by: abrtlt | last post by:
I read in Programming PHP (O'Reilly) that flock() "cannot prevent two PHP scripts running in the same web server process from accessing a file at the same time". In my case a single PHP script...
8
by: Raghu | last post by:
Is it possible to write a new file and after writing few bytes, is it possible to read from it from another file stream while write continues? Is there another steam for this type of operation? ...
0
by: soccerdad | last post by:
I'm trying to make sure that I'm using the correct .NET 2.0 mechanism to serialize objects to XML files. I've used XSD.EXE to generate classes from a 3rd party .xsd file. I can populate those...
2
by: DJ Dharme | last post by:
Hi all, I am writing a multi-threaded application in c++ running on solaris. I have a file which is updated by a single thread by appending data into the file and at same time the other threads...
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...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.