473,854 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14330
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******@sdcer a.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
XmlTextReade r 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.Cou nt];

int fooCtr = 0;

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

foos[fooCtr++] = (Foo) ser.Deserialize (
new StringReader(ob jectNode.InnerX ml));
}

-c
"vince" <vl******@sdcer a.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******@sdcer a.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
XmlTextReade r 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.Cou nt];

int fooCtr = 0;

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

foos[fooCtr++] = (Foo) ser.Deserialize (
new StringReader(ob jectNode.InnerX ml));
}

-c
"vince" <vl******@sdcer a.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******@sdcer a.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
>XmlTextReade r and advance to the position in the
>XML where 2 starts and then call:
>
>2.Deserialize( xr);
>
>-c
>
>
>.
>

.

Nov 15 '05 #4

"vince" <vl******@sdcer a.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.Cou nt];

int fooCtr = 0;

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

foos[fooCtr++] = (Foo) ser.Deserialize (
new StringReader(ob jectNode.InnerX ml));
}

-c
"vince" <vl******@sdcer a.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******@sdcer a.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
>XmlTextReade r 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******@sdcer a.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.Cou nt];
>
>int fooCtr = 0;
>
>foreach( XmlNode objectNode in objectNodes )
>{
> XmlSerializer ser = new XmlSerializer(t ypeof (Foo)); >
> foos[fooCtr++] = (Foo) ser.Deserialize (
> new StringReader(ob jectNode.InnerX ml));
>}
>
>-c
>
>
>"vince" <vl******@sdcer a.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******@sdcer a.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
>> >XmlTextReade r and advance to the position in the
>> >XML where 2 starts and then call:
>> >
>> >2.Deserialize( xr);
>> >
>> >-c
>> >
>> >
>> >.
>> >
>
>
>.
>

.

Nov 15 '05 #6

"vince" <vl******@sdcer a.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******@sdcer a.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.Cou nt];
>
>int fooCtr = 0;
>
>foreach( XmlNode objectNode in objectNodes )
>{
> XmlSerializer ser = new XmlSerializer(t ypeof (Foo)); >
> foos[fooCtr++] = (Foo) ser.Deserialize (
> new StringReader(ob jectNode.InnerX ml));
>}
>
>-c
>
>
>"vince" <vl******@sdcer a.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******@sdcer a.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
>> >XmlTextReade r 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******@sdcer a.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******@sdcer a.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.Cou nt];
>> >
>> >int fooCtr = 0;
>> >
>> >foreach( XmlNode objectNode in objectNodes )
>> >{
>> > XmlSerializer ser = new XmlSerializer(t ypeof

(Foo));
>> >
>> > foos[fooCtr++] = (Foo) ser.Deserialize (
>> > new StringReader(ob jectNode.InnerX ml));
>> >}
>> >
>> >-c
>> >
>> >
>> >"vince" <vl******@sdcer a.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******@sdcer a.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
>> >> >XmlTextReade r 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 SelectSingleNod e() 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******@sdcer a.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******@sdcer a.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******@sdcer a.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.Cou nt];
>> >
>> >int fooCtr = 0;
>> >
>> >foreach( XmlNode objectNode in objectNodes )
>> >{
>> > XmlSerializer ser = new XmlSerializer(t ypeof
(Foo));
>> >
>> > foos[fooCtr++] = (Foo) ser.Deserialize (
>> > new StringReader(ob jectNode.InnerX ml));
>> >}
>> >
>> >-c
>> >
>> >
>> >"vince" <vl******@sdcer a.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******@sdcer a.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
>> >> >XmlTextReade r 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.publi c.dotnet.genera l. 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******@sdcer a.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

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

Similar topics

4
3650
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 optional files that I could open and parse. At the end, I would easily close off the files by iterating through the dictionary. ---< CODE FOLLOWS >--- optionalfiles = {fileAreaCode: "areacode.11", fileBuild: "build.11"}
0
1380
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 object to have to serialize the two actual objects. Tu-Thach >-----Original Message----- >Can I add (append) to an xml file that already contains a
6
1712
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. So here is what i do. I create a new node and insert it in a vecotr as follows: nodeCreator = new cNode(location, skew, offset,Beaconify,nodeId,directory); gNodeVector.push_back(*nodeCreator); //now lets delete the memory we created for the node
0
1295
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. Thanks Monorom
0
1178
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 Is that correct (if so, the documentation is unclear), or how can I serialize multiple objects (of same or different type) to a single xml file thank you!
3
1876
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 the file is already opened fro writing by the StreamWriter. How can I write and read at the same time to the same file? -- Thanks Sharon G.
4
3142
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 appends text strings to an existing text file. Several clients may trigger the same script on the same web page and thus more than one PHP instance might try to open and append text to the same file at the same time. I would assume that using...
8
10547
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? Thanks. Raghu/..
0
1223
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 objects, etc. At present, I'm using the following code to create a .xml file from the root element of those objects: FileStream fs = new FileStream(targetFileName, FileMode.CreateNew); try {
2
5449
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 are reading the content written into the file. Can anybody tell me is there a performance or any other gain (except for the multex locking) by using different file descriptors in each thread for the same file rather than using a single FD with a...
0
9750
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10740
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10362
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9509
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7909
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7076
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5738
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4147
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.