If you run this code:
public class Program
{
public class MyClass
{
public 开发者_运维知识库string Text { get; set; }
}
static MyClass myClass = new MyClass();
static string Desktop = "C:\\Users\\Juan Luis\\Desktop\\";
static void Main(string[] args)
{
myClass.Text = "\r\nhello";
Console.WriteLine((int)myClass.Text[0]);
Save(Desktop + "file.xml", myClass);
myClass = Load(Desktop + "file.xml");
Console.WriteLine((int)myClass.Text[0]);
Console.Read();
}
private static MyClass Load(string fileName)
{
MyClass result = null;
using (Stream stream = File.Open(fileName, FileMode.Open))
{
XmlSerializer xmlFormatter = new XmlSerializer(typeof(MyClass));
result = (MyClass)xmlFormatter.Deserialize(stream);
}
return result;
}
private static void Save(string fileName, MyClass obj)
{
using (Stream tempFileStream = File.Create(fileName))
{
XmlSerializer xmlFormatter = new XmlSerializer(typeof(MyClass));
xmlFormatter.Serialize(tempFileStream, obj);
}
}
}
The output will be 13, 10. The XmlSerializer
is removing the carriage return. This is a problem in my case because I need to compare strings for equality in a class that gets serialized and deserialized, and this is causing two strings that are equal before serializing to be unequal after serialized. What would be the best work around?
Edit: After reading answers, this was my solution, in case it will help anyone:
public class SafeXmlSerializer : XmlSerializer
{
public SafeXmlSerializer(Type type) : base(type) { }
public new void Serialize(Stream stream, object o)
{
XmlWriterSettings ws = new XmlWriterSettings();
ws.NewLineHandling = NewLineHandling.Entitize;
using (XmlWriter xmlWriter = XmlWriter.Create(stream, ws))
{
base.Serialize(xmlWriter, o);
}
}
}
I wouldn't call it unreliable exactly: the XmlSerializer
strips white space around text inside elements. If it didn't do this then the meaning of XML documents would change according to how you formatted them in the IDE.
You could consider putting the text in a CDATA section, which will preserve the contents exactly. For example, How do you serialize a string as CDATA using XmlSerializer?
Edit: This looks to have a better explanation of where the problem lies, along with a simpler solution - How to keep XmlSerializer from killing NewLines in Strings?
精彩评论