serialize 2 objects to same xml file?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vince

    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
  • vince

    #2
    Re: serialize 2 objects to same xml file?

    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
    [color=blue]
    >-----Original Message-----
    >
    >"vince" <vlusardi@sdcer a.org> wrote in message
    >news:003401c36 352$3fb301f0$a5 01280a@phx.gbl. ..[color=green]
    >> Can I add (append) to an xml file that already[/color][/color]
    contains a[color=blue][color=green]
    >> serialized object, and be able to deserialize to either
    >> or both objects from the same file...??? How is this
    >> done...??[/color]
    >
    >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
    >
    >
    >.
    >[/color]

    Comment

    • Chad Myers

      #3
      Re: serialize 2 objects to same xml file?

      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" <vlusardi@sdcer a.org> wrote in message
      news:09ba01c363 75$9cedf5f0$a00 1280a@phx.gbl.. .[color=blue]
      > 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
      >[color=green]
      > >-----Original Message-----
      > >
      > >"vince" <vlusardi@sdcer a.org> wrote in message
      > >news:003401c36 352$3fb301f0$a5 01280a@phx.gbl. ..[color=darkred]
      > >> Can I add (append) to an xml file that already[/color][/color]
      > contains a[color=green][color=darkred]
      > >> serialized object, and be able to deserialize to either
      > >> or both objects from the same file...??? How is this
      > >> done...??[/color]
      > >
      > >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
      > >
      > >
      > >.
      > >[/color][/color]


      Comment

      • vince

        #4
        Re: serialize 2 objects to same xml file?

        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
        [color=blue]
        >-----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" <vlusardi@sdcer a.org> wrote in message
        >news:09ba01c36 375$9cedf5f0$a0 01280a@phx.gbl. ..[color=green]
        >> Chad,
        >>
        >> Thanks for the info, tried your method and got both
        >> objects to serialize to the same file, and the xml[/color][/color]
        looks[color=blue][color=green]
        >> well-formed.
        >>
        >> However, when attempting to deserialize the first[/color][/color]
        object[color=blue][color=green]
        >> 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[/color][/color]
        for[color=blue][color=green]
        >> that object in the new 2 object xml file...
        >>
        >> It would seem that the Deserialize() method is choking[/color][/color]
        on[color=blue][color=green]
        >> 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
        >>[color=darkred]
        >> >-----Original Message-----
        >> >
        >> >"vince" <vlusardi@sdcer a.org> wrote in message
        >> >news:003401c36 352$3fb301f0$a5 01280a@phx.gbl. ..
        >> >> Can I add (append) to an xml file that already[/color]
        >> contains a[color=darkred]
        >> >> serialized object, and be able to deserialize to[/color][/color][/color]
        either[color=blue][color=green][color=darkred]
        >> >> 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
        >> >
        >> >
        >> >.
        >> >[/color][/color]
        >
        >
        >.
        >[/color]

        Comment

        • Chad Myers

          #5
          Re: serialize 2 objects to same xml file?


          "vince" <vlusardi@sdcer a.org> wrote in message
          news:033601c363 7b$584f5ff0$a50 1280a@phx.gbl.. .[color=blue]
          > 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...???[/color]

          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.
          [color=blue]
          > 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?[/color]

          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

          [color=blue][color=green]
          > >-----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" <vlusardi@sdcer a.org> wrote in message
          > >news:09ba01c36 375$9cedf5f0$a0 01280a@phx.gbl. ..[color=darkred]
          > >> Chad,
          > >>
          > >> Thanks for the info, tried your method and got both
          > >> objects to serialize to the same file, and the xml[/color][/color]
          > looks[color=green][color=darkred]
          > >> well-formed.
          > >>
          > >> However, when attempting to deserialize the first[/color][/color]
          > object[color=green][color=darkred]
          > >> 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[/color][/color]
          > for[color=green][color=darkred]
          > >> that object in the new 2 object xml file...
          > >>
          > >> It would seem that the Deserialize() method is choking[/color][/color]
          > on[color=green][color=darkred]
          > >> 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" <vlusardi@sdcer a.org> wrote in message
          > >> >news:003401c36 352$3fb301f0$a5 01280a@phx.gbl. ..
          > >> >> Can I add (append) to an xml file that already
          > >> contains a
          > >> >> serialized object, and be able to deserialize to[/color][/color]
          > either[color=green][color=darkred]
          > >> >> 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
          > >> >
          > >> >
          > >> >.
          > >> >[/color]
          > >
          > >
          > >.
          > >[/color][/color]


          Comment

          • vince

            #6
            Re: serialize 2 objects to same xml file?

            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
            [color=blue]
            >-----Original Message-----
            >
            >"vince" <vlusardi@sdcer a.org> wrote in message
            >news:033601c36 37b$584f5ff0$a5 01280a@phx.gbl. ..[color=green]
            >> Chad,
            >>
            >> thanks again... so, to clarify, the workaround for the
            >> problem of having two top level tags is to manually[/color][/color]
            parse[color=blue][color=green]
            >> the xml file that was created...???[/color]
            >
            >Kinda, I guess. XML must have ONE root tag.
            >
            >You can have multiple tags underneath this, but the[/color]
            problem,[color=blue]
            >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.
            >[color=green]
            >> 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?[/color]
            >
            >Since you have multiple objects in the XML to[/color]
            deserialize,[color=blue]
            >you have to store the newly reconstituted objects[/color]
            somewhere.[color=blue]
            >You can use an ArrayList, Hashtable, or most any[/color]
            Collection[color=blue]
            >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
            >
            >[color=green][color=darkred]
            >> >-----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[/color][/color][/color]
            (Foo));[color=blue][color=green][color=darkred]
            >> >
            >> > foos[fooCtr++] = (Foo) ser.Deserialize (
            >> > new StringReader(ob jectNode.InnerX ml));
            >> >}
            >> >
            >> >-c
            >> >
            >> >
            >> >"vince" <vlusardi@sdcer a.org> wrote in message
            >> >news:09ba01c36 375$9cedf5f0$a0 01280a@phx.gbl. ..
            >> >> Chad,
            >> >>
            >> >> Thanks for the info, tried your method and got both
            >> >> objects to serialize to the same file, and the xml[/color]
            >> looks[color=darkred]
            >> >> well-formed.
            >> >>
            >> >> However, when attempting to deserialize the first[/color]
            >> object[color=darkred]
            >> >> in the file, I get an error, "There is an error in[/color][/color][/color]
            xml[color=blue][color=green][color=darkred]
            >> >> document(7,1)". .. Line 7 is the beginning of the[/color][/color][/color]
            second[color=blue][color=green][color=darkred]
            >> >> serialized object.
            >> >>
            >> >> In looking at a previous version of an xml file that
            >> >> contained only the first object, the xml is[/color][/color][/color]
            identical[color=blue][color=green]
            >> for[color=darkred]
            >> >> that object in the new 2 object xml file...
            >> >>
            >> >> It would seem that the Deserialize() method is[/color][/color][/color]
            choking[color=blue][color=green]
            >> on[color=darkred]
            >> >> 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" <vlusardi@sdcer a.org> wrote in message
            >> >> >news:003401c36 352$3fb301f0$a5 01280a@phx.gbl. ..
            >> >> >> Can I add (append) to an xml file that already
            >> >> contains a
            >> >> >> serialized object, and be able to deserialize to[/color]
            >> either[color=darkred]
            >> >> >> or both objects from the same file...??? How is[/color][/color][/color]
            this[color=blue][color=green][color=darkred]
            >> >> >> done...??
            >> >> >
            >> >> >Create an XmlTextWriter (we'll call him xw)
            >> >> >Create Two XmlSerializers initialized with each[/color][/color][/color]
            type[color=blue][color=green][color=darkred]
            >> >> >and instance you wish to serialize. (we'll call[/color][/color][/color]
            them[color=blue][color=green][color=darkred]
            >> >> >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
            >> >> >
            >> >> >
            >> >> >.
            >> >> >
            >> >
            >> >
            >> >.
            >> >[/color][/color]
            >
            >
            >.
            >[/color]

            Comment

            • Chad Myers

              #7
              Re: serialize 2 objects to same xml file?


              "vince" <vlusardi@sdcer a.org> wrote in message
              news:0a7301c363 81$5b2be440$a00 1280a@phx.gbl.. .[color=blue]
              > 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...???[/color]

              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
              [color=blue]
              >[color=green]
              > >-----Original Message-----
              > >
              > >"vince" <vlusardi@sdcer a.org> wrote in message
              > >news:033601c36 37b$584f5ff0$a5 01280a@phx.gbl. ..[color=darkred]
              > >> Chad,
              > >>
              > >> thanks again... so, to clarify, the workaround for the
              > >> problem of having two top level tags is to manually[/color][/color]
              > parse[color=green][color=darkred]
              > >> the xml file that was created...???[/color]
              > >
              > >Kinda, I guess. XML must have ONE root tag.
              > >
              > >You can have multiple tags underneath this, but the[/color]
              > problem,[color=green]
              > >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.
              > >[color=darkred]
              > >> 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?[/color]
              > >
              > >Since you have multiple objects in the XML to[/color]
              > deserialize,[color=green]
              > >you have to store the newly reconstituted objects[/color]
              > somewhere.[color=green]
              > >You can use an ArrayList, Hashtable, or most any[/color]
              > Collection[color=green]
              > >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
              > >
              > >[color=darkred]
              > >> >-----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[/color][/color]
              > (Foo));[color=green][color=darkred]
              > >> >
              > >> > foos[fooCtr++] = (Foo) ser.Deserialize (
              > >> > new StringReader(ob jectNode.InnerX ml));
              > >> >}
              > >> >
              > >> >-c
              > >> >
              > >> >
              > >> >"vince" <vlusardi@sdcer a.org> wrote in message
              > >> >news:09ba01c36 375$9cedf5f0$a0 01280a@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[/color][/color]
              > xml[color=green][color=darkred]
              > >> >> document(7,1)". .. Line 7 is the beginning of the[/color][/color]
              > second[color=green][color=darkred]
              > >> >> serialized object.
              > >> >>
              > >> >> In looking at a previous version of an xml file that
              > >> >> contained only the first object, the xml is[/color][/color]
              > identical[color=green][color=darkred]
              > >> for
              > >> >> that object in the new 2 object xml file...
              > >> >>
              > >> >> It would seem that the Deserialize() method is[/color][/color]
              > choking[color=green][color=darkred]
              > >> 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" <vlusardi@sdcer a.org> wrote in message
              > >> >> >news:003401c36 352$3fb301f0$a5 01280a@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[/color][/color]
              > this[color=green][color=darkred]
              > >> >> >> done...??
              > >> >> >
              > >> >> >Create an XmlTextWriter (we'll call him xw)
              > >> >> >Create Two XmlSerializers initialized with each[/color][/color]
              > type[color=green][color=darkred]
              > >> >> >and instance you wish to serialize. (we'll call[/color][/color]
              > them[color=green][color=darkred]
              > >> >> >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
              > >> >> >
              > >> >> >
              > >> >> >.
              > >> >> >
              > >> >
              > >> >
              > >> >.
              > >> >[/color]
              > >
              > >
              > >.
              > >[/color][/color]


              Comment

              • vince

                #8
                Re: serialize 2 objects to same xml file?

                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
                [color=blue]
                >-----Original Message-----
                >
                >"vince" <vlusardi@sdcer a.org> wrote in message
                >news:0a7301c36 381$5b2be440$a0 01280a@phx.gbl. ..[color=green]
                >> 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...???[/color]
                >
                >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
                >[color=green]
                >>[color=darkred]
                >> >-----Original Message-----
                >> >
                >> >"vince" <vlusardi@sdcer a.org> wrote in message
                >> >news:033601c36 37b$584f5ff0$a5 01280a@phx.gbl. ..
                >> >> Chad,
                >> >>
                >> >> thanks again... so, to clarify, the workaround for[/color][/color][/color]
                the[color=blue][color=green][color=darkred]
                >> >> problem of having two top level tags is to manually[/color]
                >> parse[color=darkred]
                >> >> the xml file that was created...???
                >> >
                >> >Kinda, I guess. XML must have ONE root tag.
                >> >
                >> >You can have multiple tags underneath this, but the[/color]
                >> problem,[color=darkred]
                >> >I think, is that the XmlSerializer won't understand[/color][/color][/color]
                this[color=blue][color=green][color=darkred]
                >> >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-[/color][/color][/color]
                data[color=blue][color=green][color=darkred]
                >> >and attributes to this in the future (like the[/color][/color][/color]
                version of[color=blue][color=green][color=darkred]
                >> >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[/color][/color][/color]
                snippet[color=blue][color=green][color=darkred]
                >> >> and study it for a bit...
                >> >>
                >> >> Looks like you load an intermediate array with the[/color][/color][/color]
                two[color=blue][color=green][color=darkred]
                >> >> 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[/color]
                >> deserialize,[color=darkred]
                >> >you have to store the newly reconstituted objects[/color]
                >> somewhere.[color=darkred]
                >> >You can use an ArrayList, Hashtable, or most any[/color]
                >> Collection[color=darkred]
                >> >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[/color][/color][/color]
                ("//object");[color=blue][color=green][color=darkred]
                >> >> >
                >> >> >Foo[] foos = new Foo[objectNodes.Cou nt];
                >> >> >
                >> >> >int fooCtr = 0;
                >> >> >
                >> >> >foreach( XmlNode objectNode in objectNodes )
                >> >> >{
                >> >> > XmlSerializer ser = new XmlSerializer(t ypeof[/color]
                >> (Foo));[color=darkred]
                >> >> >
                >> >> > foos[fooCtr++] = (Foo) ser.Deserialize (
                >> >> > new StringReader(ob jectNode.InnerX ml));
                >> >> >}
                >> >> >
                >> >> >-c
                >> >> >
                >> >> >
                >> >> >"vince" <vlusardi@sdcer a.org> wrote in message
                >> >> >news:09ba01c36 375$9cedf5f0$a0 01280a@phx.gbl. ..
                >> >> >> Chad,
                >> >> >>
                >> >> >> Thanks for the info, tried your method and got[/color][/color][/color]
                both[color=blue][color=green][color=darkred]
                >> >> >> objects to serialize to the same file, and the[/color][/color][/color]
                xml[color=blue][color=green][color=darkred]
                >> >> looks
                >> >> >> well-formed.
                >> >> >>
                >> >> >> However, when attempting to deserialize the first
                >> >> object
                >> >> >> in the file, I get an error, "There is an error[/color][/color][/color]
                in[color=blue][color=green]
                >> xml[color=darkred]
                >> >> >> document(7,1)". .. Line 7 is the beginning of the[/color]
                >> second[color=darkred]
                >> >> >> serialized object.
                >> >> >>
                >> >> >> In looking at a previous version of an xml file[/color][/color][/color]
                that[color=blue][color=green][color=darkred]
                >> >> >> contained only the first object, the xml is[/color]
                >> identical[color=darkred]
                >> >> for
                >> >> >> that object in the new 2 object xml file...
                >> >> >>
                >> >> >> It would seem that the Deserialize() method is[/color]
                >> choking[color=darkred]
                >> >> on
                >> >> >> the fact that there is more than one object to
                >> >> >> deserialize...
                >> >> >>
                >> >> >> Is there a step that I'm forgetting[/color][/color][/color]
                somewhere...??[color=blue][color=green][color=darkred]
                >> >> >>
                >> >> >> thanks for any help...
                >> >> >>
                >> >> >> vince
                >> >> >>
                >> >> >> >-----Original Message-----
                >> >> >> >
                >> >> >> >"vince" <vlusardi@sdcer a.org> wrote in message
                >> >> >> >news:003401c36 352$3fb301f0$a5 01280a@phx.gbl. ..
                >> >> >> >> Can I add (append) to an xml file that already
                >> >> >> contains a
                >> >> >> >> serialized object, and be able to deserialize[/color][/color][/color]
                to[color=blue][color=green][color=darkred]
                >> >> either
                >> >> >> >> or both objects from the same file...??? How[/color][/color][/color]
                is[color=blue][color=green]
                >> this[color=darkred]
                >> >> >> >> done...??
                >> >> >> >
                >> >> >> >Create an XmlTextWriter (we'll call him xw)
                >> >> >> >Create Two XmlSerializers initialized with each[/color]
                >> type[color=darkred]
                >> >> >> >and instance you wish to serialize. (we'll call[/color]
                >> them[color=darkred]
                >> >> >> >1 and 2)
                >> >> >> >
                >> >> >> >1.Serialize(xw );
                >> >> >> >2.Serialize(xw );
                >> >> >> >
                >> >> >> >The only catch is, you have to deserialize them[/color][/color][/color]
                in[color=blue][color=green][color=darkred]
                >> >> >> >the same order, or remember how long 1 is. If[/color][/color][/color]
                you[color=blue][color=green][color=darkred]
                >> >> >> >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
                >> >> >> >
                >> >> >> >
                >> >> >> >.
                >> >> >> >
                >> >> >
                >> >> >
                >> >> >.
                >> >> >
                >> >
                >> >
                >> >.
                >> >[/color][/color]
                >
                >
                >.
                >[/color]

                Comment

                • Chad Myers

                  #9
                  Re: serialize 2 objects to same xml file?

                  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" <vlusardi@sdcer a.org> wrote in message
                  news:0af101c363 8e$0a8378c0$a00 1280a@phx.gbl.. .[color=blue]
                  > 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
                  >[color=green]
                  > >-----Original Message-----
                  > >
                  > >"vince" <vlusardi@sdcer a.org> wrote in message
                  > >news:0a7301c36 381$5b2be440$a0 01280a@phx.gbl. ..[color=darkred]
                  > >> 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...???[/color]
                  > >
                  > >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
                  > >[color=darkred]
                  > >>
                  > >> >-----Original Message-----
                  > >> >
                  > >> >"vince" <vlusardi@sdcer a.org> wrote in message
                  > >> >news:033601c36 37b$584f5ff0$a5 01280a@phx.gbl. ..
                  > >> >> Chad,
                  > >> >>
                  > >> >> thanks again... so, to clarify, the workaround for[/color][/color]
                  > the[color=green][color=darkred]
                  > >> >> 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[/color][/color]
                  > this[color=green][color=darkred]
                  > >> >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-[/color][/color]
                  > data[color=green][color=darkred]
                  > >> >and attributes to this in the future (like the[/color][/color]
                  > version of[color=green][color=darkred]
                  > >> >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[/color][/color]
                  > snippet[color=green][color=darkred]
                  > >> >> and study it for a bit...
                  > >> >>
                  > >> >> Looks like you load an intermediate array with the[/color][/color]
                  > two[color=green][color=darkred]
                  > >> >> 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[/color][/color]
                  > ("//object");[color=green][color=darkred]
                  > >> >> >
                  > >> >> >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" <vlusardi@sdcer a.org> wrote in message
                  > >> >> >news:09ba01c36 375$9cedf5f0$a0 01280a@phx.gbl. ..
                  > >> >> >> Chad,
                  > >> >> >>
                  > >> >> >> Thanks for the info, tried your method and got[/color][/color]
                  > both[color=green][color=darkred]
                  > >> >> >> objects to serialize to the same file, and the[/color][/color]
                  > xml[color=green][color=darkred]
                  > >> >> looks
                  > >> >> >> well-formed.
                  > >> >> >>
                  > >> >> >> However, when attempting to deserialize the first
                  > >> >> object
                  > >> >> >> in the file, I get an error, "There is an error[/color][/color]
                  > in[color=green][color=darkred]
                  > >> xml
                  > >> >> >> document(7,1)". .. Line 7 is the beginning of the
                  > >> second
                  > >> >> >> serialized object.
                  > >> >> >>
                  > >> >> >> In looking at a previous version of an xml file[/color][/color]
                  > that[color=green][color=darkred]
                  > >> >> >> 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[/color][/color]
                  > somewhere...??[color=green][color=darkred]
                  > >> >> >>
                  > >> >> >> thanks for any help...
                  > >> >> >>
                  > >> >> >> vince
                  > >> >> >>
                  > >> >> >> >-----Original Message-----
                  > >> >> >> >
                  > >> >> >> >"vince" <vlusardi@sdcer a.org> wrote in message
                  > >> >> >> >news:003401c36 352$3fb301f0$a5 01280a@phx.gbl. ..
                  > >> >> >> >> Can I add (append) to an xml file that already
                  > >> >> >> contains a
                  > >> >> >> >> serialized object, and be able to deserialize[/color][/color]
                  > to[color=green][color=darkred]
                  > >> >> either
                  > >> >> >> >> or both objects from the same file...??? How[/color][/color]
                  > is[color=green][color=darkred]
                  > >> 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[/color][/color]
                  > in[color=green][color=darkred]
                  > >> >> >> >the same order, or remember how long 1 is. If[/color][/color]
                  > you[color=green][color=darkred]
                  > >> >> >> >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
                  > >> >> >> >
                  > >> >> >> >
                  > >> >> >> >.
                  > >> >> >> >
                  > >> >> >
                  > >> >> >
                  > >> >> >.
                  > >> >> >
                  > >> >
                  > >> >
                  > >> >.
                  > >> >[/color]
                  > >
                  > >
                  > >.
                  > >[/color][/color]


                  Comment

                  • Robert Jacobson

                    #10
                    Re: serialize 2 objects to same xml file?

                    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" <vlusardi@sdcer a.org> wrote in message
                    news:003401c363 52$3fb301f0$a50 1280a@phx.gbl.. .[color=blue]
                    > 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[/color]


                    Comment

                    • Robert Jacobson

                      #11
                      Re: serialize 2 objects to same xml file?

                      > .... in microsoft.publi c.dotnet.genera l.

                      Correction: microsoft.publi c.dotnet.vb


                      Comment

                      • vince

                        #12
                        Re: serialize 2 objects to same xml file?

                        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(t ypeof(object[]));
                        TextWriter tw = new StreamWriter("c :\\backup\\" +
                        textBox1.Text + ".xml");

                        x2.Serialize(tw , jobArray);

                        The exception thrown is "Job.JobFil e 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..

                        [color=blue]
                        >-----Original Message-----
                        >Vince,
                        >
                        >I replied to your question in[/color]
                        microsoft.publi c.dotnet.genera l. In the[color=blue]
                        >future, please don't repost the same question to[/color]
                        multiple groups.[color=blue]
                        >
                        >There's no need to use strange workarounds to get this[/color]
                        done. Just define an[color=blue]
                        >array of objects (or ArrayList, or strongly-typed[/color]
                        collection), place both[color=blue]
                        >items in the array, and serialize/deserialize the[/color]
                        array. It will work fine.[color=blue]
                        >
                        >--Robert Jacobson
                        >
                        >
                        >
                        >
                        >
                        >"vince" <vlusardi@sdcer a.org> wrote in message
                        >news:003401c36 352$3fb301f0$a5 01280a@phx.gbl. ..[color=green]
                        >> Can I add (append) to an xml file that already[/color][/color]
                        contains a[color=blue][color=green]
                        >> serialized object, and be able to deserialize to either
                        >> or both objects from the same file...??? How is this
                        >> done...??
                        >>
                        >> thanks,
                        >>
                        >> vince[/color]
                        >
                        >
                        >.
                        >[/color]

                        Comment

                        • vince

                          #13
                          Re: serialize 2 objects to same xml file?

                          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
                          [color=blue]
                          >-----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
                          >XmlSerialize r x2 = new XmlSerializer(t ypeof(object[]));
                          >TextWriter tw = new StreamWriter("c :\\backup\\" +
                          >textBox1.Tex t + ".xml");
                          >
                          >x2.Serialize(t w, jobArray);
                          >
                          >The exception thrown is "Job.JobFil e 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..
                          >
                          >[color=green]
                          >>-----Original Message-----
                          >>Vince,
                          >>
                          >>I replied to your question in[/color]
                          >microsoft.publ ic.dotnet.gener al. In the[color=green]
                          >>future, please don't repost the same question to[/color]
                          >multiple groups.[color=green]
                          >>
                          >>There's no need to use strange workarounds to get this[/color]
                          >done. Just define an[color=green]
                          >>array of objects (or ArrayList, or strongly-typed[/color]
                          >collection), place both[color=green]
                          >>items in the array, and serialize/deserialize the[/color]
                          >array. It will work fine.[color=green]
                          >>
                          >>--Robert Jacobson
                          >>
                          >>
                          >>
                          >>
                          >>
                          >>"vince" <vlusardi@sdcer a.org> wrote in message
                          >>news:003401c3 6352$3fb301f0$a 501280a@phx.gbl ...[color=darkred]
                          >>> Can I add (append) to an xml file that already[/color][/color]
                          >contains a[color=green][color=darkred]
                          >>> serialized object, and be able to deserialize to[/color][/color][/color]
                          either[color=blue][color=green][color=darkred]
                          >>> or both objects from the same file...??? How is this
                          >>> done...??
                          >>>
                          >>> thanks,
                          >>>
                          >>> vince[/color]
                          >>
                          >>
                          >>.
                          >>[/color]
                          >.
                          >[/color]

                          Comment

                          • Robert Jacobson

                            #14
                            Re: serialize 2 objects to same xml file?

                            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:




                            Hope this helps,
                            Robert Jacobson


                            using System;
                            using System.Xml.Seri alization;
                            using System.IO;
                            using System.Diagnost ics;

                            namespace ConsoleApplicat ion1
                            {
                            class Class1
                            {
                            [STAThread]
                            static void Main(string[] args)
                            {
                            SerializationTe st();
                            }

                            static void SerializationTe st()
                            {
                            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(j obArray.GetType ());
                            StreamWriter sw = new StreamWriter("T est.xml");

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


                            }
                            }

                            public class DummyJob
                            {
                            public string Name;
                            }

                            }


                            "vince" <vlusardi@sdcer a.org> wrote in message
                            news:024501c364 3f$21699270$a40 1280a@phx.gbl.. .[color=blue]
                            > 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
                            >[color=green]
                            > >-----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
                            > >XmlSerialize r x2 = new XmlSerializer(t ypeof(object[]));
                            > >TextWriter tw = new StreamWriter("c :\\backup\\" +
                            > >textBox1.Tex t + ".xml");
                            > >
                            > >x2.Serialize(t w, jobArray);
                            > >
                            > >The exception thrown is "Job.JobFil e 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..
                            > >
                            > >[color=darkred]
                            > >>-----Original Message-----
                            > >>Vince,
                            > >>
                            > >>I replied to your question in[/color]
                            > >microsoft.publ ic.dotnet.gener al. In the[color=darkred]
                            > >>future, please don't repost the same question to[/color]
                            > >multiple groups.[color=darkred]
                            > >>
                            > >>There's no need to use strange workarounds to get this[/color]
                            > >done. Just define an[color=darkred]
                            > >>array of objects (or ArrayList, or strongly-typed[/color]
                            > >collection), place both[color=darkred]
                            > >>items in the array, and serialize/deserialize the[/color]
                            > >array. It will work fine.[color=darkred]
                            > >>
                            > >>--Robert Jacobson
                            > >>
                            > >>
                            > >>
                            > >>
                            > >>
                            > >>"vince" <vlusardi@sdcer a.org> wrote in message
                            > >>news:003401c3 6352$3fb301f0$a 501280a@phx.gbl ...
                            > >>> Can I add (append) to an xml file that already[/color]
                            > >contains a[color=darkred]
                            > >>> serialized object, and be able to deserialize to[/color][/color]
                            > either[color=green][color=darkred]
                            > >>> or both objects from the same file...??? How is this
                            > >>> done...??
                            > >>>
                            > >>> thanks,
                            > >>>
                            > >>> vince
                            > >>
                            > >>
                            > >>.
                            > >>[/color]
                            > >.
                            > >[/color][/color]


                            Comment

                            • vince

                              #15
                              Re: serialize 2 objects to same xml file?

                              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(j obArray.GetType ());
                              TextWriter writer = new StreamWriter("c :\\backup\\" +
                              textBox1.Text + ".xml");
                              x.Serialize(wri ter, jobArray);

                              Thanks for your help,

                              Vince Lusardi
                              [color=blue]
                              >-----Original Message-----
                              >You're right -- it turns out you can't just stuff an[/color]
                              Object[] array with[color=blue]
                              >custom classes and expect it to serailize correctly.[/color]
                              The reason is that the[color=blue]
                              >XmlSerialize r is kind of dumb... it needs to know what[/color]
                              type of object is[color=blue]
                              >being stored in the array so that it can deserialize it[/color]
                              correctly later on.[color=blue]
                              >Sorry for leading you astray. <g>
                              >
                              >The easiest way around this is to use a strongly-typed[/color]
                              array or collection.[color=blue]
                              >So, it should work if you replace the line "object[][/color]
                              jobArray = new[color=blue]
                              >object[2];" with "jobFile[] jobArray = new jobFile[2];"[/color]
                              I have some sample[color=blue]
                              >code below. Alternatively, you could define a strongly-[/color]
                              typed JobFile[color=blue]
                              >collection by inheriting from CollectionBase.
                              >
                              >Another alternative is to use the SOAP serializer or[/color]
                              binary serializer[color=blue]
                              >instead. Both are more robust than the XmlSerializer,[/color]
                              and should be able to[color=blue]
                              >serialize Object[] arrays correctly. The tradeoff is[/color]
                              that the output from[color=blue]
                              >those serializers are not as easily readable (for human[/color]
                              eyes) than the clean[color=blue]
                              >XML generated by the XmlSerailizer. Check out these[/color]
                              articles:[color=blue]
                              >
                              >http://msdn.microsoft.com/library/default.asp?[/color]
                              url=/library/en-us/dndotnet/html/objserializ.asp[color=blue]
                              >http://msdn.microsoft.com/library/default.asp?[/color]
                              url=/library/en-us/dnadvnet/html/vbnet09252001.a sp[color=blue]
                              >
                              >Hope this helps,
                              >Robert Jacobson
                              >
                              >
                              >using System;
                              >using System.Xml.Seri alization;
                              >using System.IO;
                              >using System.Diagnost ics;
                              >
                              >namespace ConsoleApplicat ion1
                              >{
                              > class Class1
                              > {
                              > [STAThread]
                              > static void Main(string[] args)
                              > {
                              > SerializationTe st();
                              > }
                              >
                              > static void SerializationTe st()
                              > {
                              > 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[/color]
                              (jobArray.GetTy pe());[color=blue]
                              > StreamWriter sw = new StreamWriter("T est.xml");
                              >
                              > serializer.Seri alize(sw, jobArray);
                              > sw.Close();
                              > }
                              > catch (Exception ex)
                              > {
                              > Debug.WriteLine (ex.ToString()) ;
                              > }
                              >
                              >
                              > }
                              > }
                              >
                              > public class DummyJob
                              > {
                              > public string Name;
                              > }
                              >
                              >}
                              >
                              >
                              >"vince" <vlusardi@sdcer a.org> wrote in message
                              >news:024501c36 43f$21699270$a4 01280a@phx.gbl. ..[color=green]
                              >> 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
                              >>[color=darkred]
                              >> >-----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
                              >> >XmlSerialize r x2 = new XmlSerializer(t ypeof(object[/color][/color][/color]
                              []));[color=blue][color=green][color=darkred]
                              >> >TextWriter tw = new StreamWriter("c :\\backup\\" +
                              >> >textBox1.Tex t + ".xml");
                              >> >
                              >> >x2.Serialize(t w, jobArray);
                              >> >
                              >> >The exception thrown is "Job.JobFil e may not be used[/color][/color][/color]
                              in[color=blue][color=green][color=darkred]
                              >> >this context".... Job is the namespace, and theJob is[/color][/color][/color]
                              an[color=blue][color=green][color=darkred]
                              >> >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.publ ic.dotnet.gener al. In the
                              >> >>future, please don't repost the same question to
                              >> >multiple groups.
                              >> >>
                              >> >>There's no need to use strange workarounds to get[/color][/color][/color]
                              this[color=blue][color=green][color=darkred]
                              >> >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" <vlusardi@sdcer a.org> wrote in message
                              >> >>news:003401c3 6352$3fb301f0$a 501280a@phx.gbl ...
                              >> >>> Can I add (append) to an xml file that already
                              >> >contains a
                              >> >>> serialized object, and be able to deserialize to[/color]
                              >> either[color=darkred]
                              >> >>> or both objects from the same file...??? How is[/color][/color][/color]
                              this[color=blue][color=green][color=darkred]
                              >> >>> done...??
                              >> >>>
                              >> >>> thanks,
                              >> >>>
                              >> >>> vince
                              >> >>
                              >> >>
                              >> >>.
                              >> >>
                              >> >.
                              >> >[/color][/color]
                              >
                              >
                              >.
                              >[/color]

                              Comment

                              Working...