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. Problem with XML-Serializer

Problem with XML-Serializer

Scheduled Pinned Locked Moved C#
xmlhelpquestion
4 Posts 2 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.
  • P Offline
    P Offline
    Philip F
    wrote on last edited by
    #1

    Hi everybody! I have a problem with the XmlSerializer: When serializing a property (string) containing only a TAB "\t", I get only an empty string. The serialized object looks like this: " " (There really is a tab ;) ) Here's my property: protected string separator = ";"; public string Separator { get { return separator; } set { separator = value; } } I know I could make a char property, but this would be a workaround for this problem, not the solution. Any ideas? Thanks, Phil I won’t not use no double negatives.

    L 1 Reply Last reply
    0
    • P Philip F

      Hi everybody! I have a problem with the XmlSerializer: When serializing a property (string) containing only a TAB "\t", I get only an empty string. The serialized object looks like this: " " (There really is a tab ;) ) Here's my property: protected string separator = ";"; public string Separator { get { return separator; } set { separator = value; } } I know I could make a char property, but this would be a workaround for this problem, not the solution. Any ideas? Thanks, Phil I won’t not use no double negatives.

      L Offline
      L Offline
      Le centriste
      wrote on last edited by
      #2

      In XML, all whitespaces are treated as a single space, even when there are many together. When there is only whitespaces, it is treated as an empty string. Maybe you could try to put it in a CDATA section (if you have control over the XML itself).

      P 1 Reply Last reply
      0
      • L Le centriste

        In XML, all whitespaces are treated as a single space, even when there are many together. When there is only whitespaces, it is treated as an empty string. Maybe you could try to put it in a CDATA section (if you have control over the XML itself).

        P Offline
        P Offline
        Philip F
        wrote on last edited by
        #3

        Hi, Merci beaucoup @Le Centriste. I tried it with CDATA, and it actually worked. But I had to wrap my property, and that wasn't really useful because I could also have created a (better accessible) char[]. (see http://geekswithblogs.net/cmartin/archive/2005/11/30/61705.aspx[^] for more information.) But I found a better solution. Instead of using a standard XmlSerializer (like this):

        XmlSerializer serializer = new XmlSerializer(typeof(MyObjectType));
        reader = new FileStream(filepath, FileMode.Open);

        myObject = ((MyObjectType)serializer.Deserialize(reader));

        I changed the settings:

        XmlSerializer serializer = new XmlSerializer(typeof(MyObjectType));
        reader = new FileStream(filepath, FileMode.Open);
        System.Xml.XmlReaderSettings xmlSettings = new System.Xml.XmlReaderSettings();
        xmlSettings.IgnoreWhitespace = false;
        System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(reader,xmlSettings);

        myObject = ((MyObjectType)serializer.Deserialize(xmlReader));

        This way, I can use Whitespaces as I like to ;) But thanks for your answer! :) Phil

        I won’t not use no double negatives.

        L 1 Reply Last reply
        0
        • P Philip F

          Hi, Merci beaucoup @Le Centriste. I tried it with CDATA, and it actually worked. But I had to wrap my property, and that wasn't really useful because I could also have created a (better accessible) char[]. (see http://geekswithblogs.net/cmartin/archive/2005/11/30/61705.aspx[^] for more information.) But I found a better solution. Instead of using a standard XmlSerializer (like this):

          XmlSerializer serializer = new XmlSerializer(typeof(MyObjectType));
          reader = new FileStream(filepath, FileMode.Open);

          myObject = ((MyObjectType)serializer.Deserialize(reader));

          I changed the settings:

          XmlSerializer serializer = new XmlSerializer(typeof(MyObjectType));
          reader = new FileStream(filepath, FileMode.Open);
          System.Xml.XmlReaderSettings xmlSettings = new System.Xml.XmlReaderSettings();
          xmlSettings.IgnoreWhitespace = false;
          System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(reader,xmlSettings);

          myObject = ((MyObjectType)serializer.Deserialize(xmlReader));

          This way, I can use Whitespaces as I like to ;) But thanks for your answer! :) Phil

          I won’t not use no double negatives.

          L Offline
          L Offline
          Le centriste
          wrote on last edited by
          #4

          Very good!

          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