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. string serialization

string serialization

Scheduled Pinned Locked Moved C#
questionxmljson
2 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.
  • B Offline
    B Offline
    balkang
    wrote on last edited by
    #1

    Hi, I have a class that has a string attribute. I set this attribute to " " (a space) and serialize the instance to an xml file. When I deserialize that instance the value of that string attribute is null, not space.. How can I do that??

    public class MyClass
    {
    private string str;

    public string Str
    {
    	get{return str;}
    	set{str = value;}
    }
    

    }
    [STAThread]
    static void Main(string[] args)
    {
    MyClass mycls = new MyClass();
    mycls.Str = " "; //a space assigned

    //Serialize it
    TextWriter tw = new StreamWriter("str.xml"); 
    XmlSerializer sr = new XmlSerializer(typeof(MyClass)); 
    sr.Serialize(tw, mycls);
    tw.Close();
    
    //Deserialize
    FileStream fs = new FileStream("str.xml", FileMode.Open); 
    XmlSerializer sr2 = new XmlSerializer(typeof(MyClass)); 
    MyClass mycls2 = (MyClass)sr2.Deserialize(fs);
    fs.Close();
    Console.WriteLine("\*\*\*" + mycls2.Str + "\*\*\*" );
    

    }

    output: ******

    A 1 Reply Last reply
    0
    • B balkang

      Hi, I have a class that has a string attribute. I set this attribute to " " (a space) and serialize the instance to an xml file. When I deserialize that instance the value of that string attribute is null, not space.. How can I do that??

      public class MyClass
      {
      private string str;

      public string Str
      {
      	get{return str;}
      	set{str = value;}
      }
      

      }
      [STAThread]
      static void Main(string[] args)
      {
      MyClass mycls = new MyClass();
      mycls.Str = " "; //a space assigned

      //Serialize it
      TextWriter tw = new StreamWriter("str.xml"); 
      XmlSerializer sr = new XmlSerializer(typeof(MyClass)); 
      sr.Serialize(tw, mycls);
      tw.Close();
      
      //Deserialize
      FileStream fs = new FileStream("str.xml", FileMode.Open); 
      XmlSerializer sr2 = new XmlSerializer(typeof(MyClass)); 
      MyClass mycls2 = (MyClass)sr2.Deserialize(fs);
      fs.Close();
      Console.WriteLine("\*\*\*" + mycls2.Str + "\*\*\*" );
      

      }

      output: ******

      A Offline
      A Offline
      aamironline
      wrote on last edited by
      #2

      Try this out!!! . . . FileStream fs = new FileStream("str.xml", FileMode.Open); XmlTextReader reader = new XmlTextReader(fs); XmlSerializer sr2 = new XmlSerializer(typeof(MyClass)); MyClass mycls2 = (MyClass)sr2.Deserialize(reader); . . .

      M Aamir Maniar aamirOnline.com

      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