string serialization
-
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: ******
-
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: ******
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