Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Quick question involving xml deserialization and strings

Quick question involving xml deserialization and strings

Scheduled Pinned Locked Moved C#
questionxml
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ruanr
    wrote on last edited by
    #1

    Hi, I recieve a chunk of XML as a string that i want to then deserialize, but none of the XmlSerializer.Deserialize overloads accept a string as parameter, mostly only streams, textreaders or xmlreaders. Surely there has to be a quick way to convert a string to one of those formats?

    O J P 3 Replies Last reply
    0
    • R ruanr

      Hi, I recieve a chunk of XML as a string that i want to then deserialize, but none of the XmlSerializer.Deserialize overloads accept a string as parameter, mostly only streams, textreaders or xmlreaders. Surely there has to be a quick way to convert a string to one of those formats?

      O Offline
      O Offline
      originSH
      wrote on last edited by
      #2

      The streams live in System.IO, have a look there.

      1 Reply Last reply
      0
      • R ruanr

        Hi, I recieve a chunk of XML as a string that i want to then deserialize, but none of the XmlSerializer.Deserialize overloads accept a string as parameter, mostly only streams, textreaders or xmlreaders. Surely there has to be a quick way to convert a string to one of those formats?

        J Offline
        J Offline
        Jon Hulatt
        wrote on last edited by
        #3

        I don't think there's a smart way of doing this. You could allocate a byte array, create a MemoryStream on it, and then write your string to the memory stream. Then you can deserialize from that.

        using System.Beer;

        O 1 Reply Last reply
        0
        • R ruanr

          Hi, I recieve a chunk of XML as a string that i want to then deserialize, but none of the XmlSerializer.Deserialize overloads accept a string as parameter, mostly only streams, textreaders or xmlreaders. Surely there has to be a quick way to convert a string to one of those formats?

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          Off the top of my head, you could try this:

          public T FromString<T>(string serializedObject) where T : new()
          {
            XmlSerializer xmlSer = new XmlSerializer(typeof(T));
            StringReader sr = new StringReader(serializedObject);
            XmlTextReader stream = new XmlTextReader(sr);
          
            try
            {
              T obj = (T)xmlSer.Deserialize(stream);
              return obj;
            }
            catch(Exception ex)
            {
              // Log ex...
            }
            finally
            {
              sr.Close();
              stream.Close();
            }
          }
          

          Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.

          R 1 Reply Last reply
          0
          • P Pete OHanlon

            Off the top of my head, you could try this:

            public T FromString<T>(string serializedObject) where T : new()
            {
              XmlSerializer xmlSer = new XmlSerializer(typeof(T));
              StringReader sr = new StringReader(serializedObject);
              XmlTextReader stream = new XmlTextReader(sr);
            
              try
              {
                T obj = (T)xmlSer.Deserialize(stream);
                return obj;
              }
              catch(Exception ex)
              {
                // Log ex...
              }
              finally
              {
                sr.Close();
                stream.Close();
              }
            }
            

            Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.

            R Offline
            R Offline
            ruanr
            wrote on last edited by
            #5

            beautiful, thanks a lot :)

            P 1 Reply Last reply
            0
            • R ruanr

              beautiful, thanks a lot :)

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              Glad to help.:-D

              Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.

              1 Reply Last reply
              0
              • J Jon Hulatt

                I don't think there's a smart way of doing this. You could allocate a byte array, create a MemoryStream on it, and then write your string to the memory stream. Then you can deserialize from that.

                using System.Beer;

                O Offline
                O Offline
                originSH
                wrote on last edited by
                #7

                Jon Hulatt wrote:

                I don't think there's a smart way of doing this.

                System.IO.StringReader

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups