Problem with XML-Serializer
-
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.
-
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.
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).
-
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).
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.
-
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.
Very good!