Serialization

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

    #1

    Serialization

    I have a problem with Xml and Serialization.

    I use serialization to get Xml as a result. All
    Serialization examples in Microsoft documentation writes
    the result to a file.

    I don't want a file, I want an XML-object (XmlNode)
    instead that I can pass as a parameter to another method.

    How can I do that?

    Example code:

    XmlSerializer mySerializer = new XmlSerializer(t ypeof
    (myClass));
    TextWriter myWriter = new StreamWriter(@" C:\myfile.xml") ;

    mySerializer.Se rialize(myXmlWr iter, myNode);
    myWriter.Close( );

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Serialization

    Janne,

    Instead of using the constructor for a StreamWriter that takes a file
    name, use a MemoryStream, and pass that, like this:

    // The memory stream that will hold the xml.
    MemoryStream pobjStream = new MemoryStream();

    // Create the StreamWriter around the memory stream.
    TextWriter myWriter = new StreamWriter(po bjStream);

    // Now save to the memory stream.
    mySerializer.Se rialize(myXmlWr iter, myNode);

    Now, if you want to get that into an Xml document, you can call the Load
    method, passing a stream to it.

    // Create the xml document.
    XmlDocument pobjXmlDoc = new XmlDocument();

    // Load the content.
    pobjXmlDoc.Load (pobjStream);

    XmlDocument derives from XmlNode so you can just pass that around to
    anything that accepts an XmlNode instance.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Janne" <anonymous@disc ussions.microso ft.com> wrote in message
    news:087e01c39f a5$37bbca60$a60 1280a@phx.gbl.. .[color=blue]
    > I have a problem with Xml and Serialization.
    >
    > I use serialization to get Xml as a result. All
    > Serialization examples in Microsoft documentation writes
    > the result to a file.
    >
    > I don't want a file, I want an XML-object (XmlNode)
    > instead that I can pass as a parameter to another method.
    >
    > How can I do that?
    >
    > Example code:
    >
    > XmlSerializer mySerializer = new XmlSerializer(t ypeof
    > (myClass));
    > TextWriter myWriter = new StreamWriter(@" C:\myfile.xml") ;
    >
    > mySerializer.Se rialize(myXmlWr iter, myNode);
    > myWriter.Close( );
    >[/color]


    Comment

    • Janne

      #3
      Re: Serialization

      Nicholas!

      Thank you for your help but it still won't work.

      Is there a way to check the Stream-object or the Writer-
      object? The load-command gives me an empty XmlDocument.

      Take a look at the code and check please.
      [color=blue]
      >MemoryStream pobjStream = new MemoryStream();
      >
      >TextWriter myWriter = new StreamWriter(po bjStream);
      >
      >mySerializer.S erialize(myWrit er, myNode);
      >XmlDocument pobjXmlDoc = new XmlDocument();
      >
      >pobjXmlDoc.Loa d(pobjStream);[/color]



      [color=blue]
      >-----Original Message-----
      >Janne,
      >
      > Instead of using the constructor for a StreamWriter[/color]
      that takes a file[color=blue]
      >name, use a MemoryStream, and pass that, like this:
      >
      >// The memory stream that will hold the xml.
      >MemoryStream pobjStream = new MemoryStream();
      >
      >// Create the StreamWriter around the memory stream.
      >TextWriter myWriter = new StreamWriter(po bjStream);
      >
      >// Now save to the memory stream.
      >mySerializer.S erialize(myXmlW riter, myNode);
      >
      > Now, if you want to get that into an Xml document,[/color]
      you can call the Load[color=blue]
      >method, passing a stream to it.
      >
      >// Create the xml document.
      >XmlDocument pobjXmlDoc = new XmlDocument();
      >
      >// Load the content.
      >pobjXmlDoc.Loa d(pobjStream);
      >
      > XmlDocument derives from XmlNode so you can just[/color]
      pass that around to[color=blue]
      >anything that accepts an XmlNode instance.
      >
      > Hope this helps.
      >
      >
      >--
      > - Nicholas Paldino [.NET/C# MVP]
      > - mvp@spam.guard. caspershouse.co m
      >
      >"Janne" <anonymous@disc ussions.microso ft.com> wrote in[/color]
      message[color=blue]
      >news:087e01c39 fa5$37bbca60$a6 01280a@phx.gbl. ..[color=green]
      >> I have a problem with Xml and Serialization.
      >>
      >> I use serialization to get Xml as a result. All
      >> Serialization examples in Microsoft documentation[/color][/color]
      writes[color=blue][color=green]
      >> the result to a file.
      >>
      >> I don't want a file, I want an XML-object (XmlNode)
      >> instead that I can pass as a parameter to another[/color][/color]
      method.[color=blue][color=green]
      >>
      >> How can I do that?
      >>
      >> Example code:
      >>
      >> XmlSerializer mySerializer = new XmlSerializer(t ypeof
      >> (myClass));
      >> TextWriter myWriter = new StreamWriter[/color][/color]
      (@"C:\myfile.xm l");[color=blue][color=green]
      >>
      >> mySerializer.Se rialize(myXmlWr iter, myNode);
      >> myWriter.Close( );
      >>[/color]
      >
      >
      >.
      >[/color]

      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Serialization

        Janne,

        The MemoryStream class has a ToArray method you can use to return the
        byte array that represents the bytes in the stream.


        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m

        "Janne" <anonymous@disc ussions.microso ft.com> wrote in message
        news:059901c39f c0$67ded5a0$a00 1280a@phx.gbl.. .[color=blue]
        > Nicholas!
        >
        > Thank you for your help but it still won't work.
        >
        > Is there a way to check the Stream-object or the Writer-
        > object? The load-command gives me an empty XmlDocument.
        >
        > Take a look at the code and check please.
        >[color=green]
        > >MemoryStream pobjStream = new MemoryStream();
        > >
        > >TextWriter myWriter = new StreamWriter(po bjStream);
        > >
        > >mySerializer.S erialize(myWrit er, myNode);
        > >XmlDocument pobjXmlDoc = new XmlDocument();
        > >
        > >pobjXmlDoc.Loa d(pobjStream);[/color]
        >
        >
        >
        >[color=green]
        > >-----Original Message-----
        > >Janne,
        > >
        > > Instead of using the constructor for a StreamWriter[/color]
        > that takes a file[color=green]
        > >name, use a MemoryStream, and pass that, like this:
        > >
        > >// The memory stream that will hold the xml.
        > >MemoryStream pobjStream = new MemoryStream();
        > >
        > >// Create the StreamWriter around the memory stream.
        > >TextWriter myWriter = new StreamWriter(po bjStream);
        > >
        > >// Now save to the memory stream.
        > >mySerializer.S erialize(myXmlW riter, myNode);
        > >
        > > Now, if you want to get that into an Xml document,[/color]
        > you can call the Load[color=green]
        > >method, passing a stream to it.
        > >
        > >// Create the xml document.
        > >XmlDocument pobjXmlDoc = new XmlDocument();
        > >
        > >// Load the content.
        > >pobjXmlDoc.Loa d(pobjStream);
        > >
        > > XmlDocument derives from XmlNode so you can just[/color]
        > pass that around to[color=green]
        > >anything that accepts an XmlNode instance.
        > >
        > > Hope this helps.
        > >
        > >
        > >--
        > > - Nicholas Paldino [.NET/C# MVP]
        > > - mvp@spam.guard. caspershouse.co m
        > >
        > >"Janne" <anonymous@disc ussions.microso ft.com> wrote in[/color]
        > message[color=green]
        > >news:087e01c39 fa5$37bbca60$a6 01280a@phx.gbl. ..[color=darkred]
        > >> I have a problem with Xml and Serialization.
        > >>
        > >> I use serialization to get Xml as a result. All
        > >> Serialization examples in Microsoft documentation[/color][/color]
        > writes[color=green][color=darkred]
        > >> the result to a file.
        > >>
        > >> I don't want a file, I want an XML-object (XmlNode)
        > >> instead that I can pass as a parameter to another[/color][/color]
        > method.[color=green][color=darkred]
        > >>
        > >> How can I do that?
        > >>
        > >> Example code:
        > >>
        > >> XmlSerializer mySerializer = new XmlSerializer(t ypeof
        > >> (myClass));
        > >> TextWriter myWriter = new StreamWriter[/color][/color]
        > (@"C:\myfile.xm l");[color=green][color=darkred]
        > >>
        > >> mySerializer.Se rialize(myXmlWr iter, myNode);
        > >> myWriter.Close( );
        > >>[/color]
        > >
        > >
        > >.
        > >[/color][/color]


        Comment

        Working...