XML反序列化“标准化"行的结尾,如何制止它? (.网) [英] XML deserialization 'standardising' line endings, how to stop it? (.NET)

查看:94
本文介绍了XML反序列化“标准化"行的结尾,如何制止它? (.网)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有标记为[XmlText]的属性的类,该类接受多行输入.在我的XML文件中,我已验证文本内容内的行尾实际上是"\r\n",与文件的其余部分相同.

I have a class with a property marked with [XmlText], that accepts multiline input. In my XML file, I've verified that the line endings inside the text content are infact "\r\n", the same as the rest of the file.

我用于反序列化的代码是:

The code I'm using to deserialize is:

XmlSerializer configSerializer = new XmlSerializer(typeof(WorldList));
string file = "test.xml";

if (File.Exists(file))
{
    using (TextReader r = new StreamReader(file))
    {
        foo = configSerializer.Deserialize(r);
    }
}

但是,在[XmlText]属性的设置器中,value已经以"\n"作为行尾.这很烦人的主要原因是因为"\n"行尾在TextBox中无法正确显示...我不确定是哪一部分引起了问题,但我认为这里的人也许可以摆脱一些麻烦视情况而定.

But, inside the setter for the [XmlText] property, value already has "\n" as a line ending. The main reason this is annoying is because "\n" line endings don't display properly in a TextBox... I'm not exactly sure which part is causing the problems, but I thought someone here might be able to shed some light on the situation.

推荐答案

我更改了示例代码,以使将代码快速放入C#开发环境中变得更加容易.我还故意不使用语句-它只是示例代码.

I've change the sample code to make it easier to quickly put the code into a C# development environment. I've also deliberately not included using statements- it is only sample code.

在此示例中,我们具有要序列化的跟随类:

For the example we have the follow class we wish to serialize:

   public class DataToSerialize
   {
       public string Name { get; set; }
   }

如果我们尝试以您描述打印"Same"的行的方式进行序列化和反序列化,则将不会执行(我假设代码将在Windows上使用Environment.NewLine运行,请替换为"\ r \ n(如果不是):

If we try to serialize and deserialize this in the way you describe the line where "Same" gets printed out will not get executed (I'm assuming the code will run on Windows with Environment.NewLine, replace with "\r\n" if you are not):

    DataToSerialize test = new DataToSerialize();
    test.Name = "TestData" + Environment.NewLine;
    XmlSerializer configSerializer = new XmlSerializer(typeof(DataToSerialize));
    MemoryStream ms = new MemoryStream();
    StreamWriter sw = new StreamWriter(ms);
    configSerializer.Serialize(sw, test);
    ms.Position = 0;
    DataToSerialize deserialized = (DataToSerialize)configSerializer.Deserialize(ms);
    if (deserialized.Name.Equals("TestData" + Environment.NewLine))
    {
        Console.WriteLine("Same");
    }

但是,可以通过手动将XmlTextReader分配给序列化程序并将其Normalization属性设置为false(序列化程序默认使用的设置为true)来解决此问题:

However this can be fixed, by manually assigning an XmlTextReader to the serializer, with it's Normalization property set to false (the one used by default in the serializer is set to true):

    DataToSerialize test = new DataToSerialize();
    test.Name = "TestData" + Environment.NewLine;
    XmlSerializer configSerializer = new XmlSerializer(typeof(DataToSerialize));
    MemoryStream ms = new MemoryStream();
    StreamWriter sw = new StreamWriter(ms);

    configSerializer.Serialize(sw, test);
    ms.Position = 0;
    XmlTextReader reader = new XmlTextReader(ms);
    reader.Normalization = false;
    DataToSerialize deserialized = (DataToSerialize)configSerializer.Deserialize(reader);
    if (deserialized.Name.Equals("TestData" + Environment.NewLine))
    {
        Console.WriteLine("Same");
    }

现在将打印出相同的内容,除非我错了,否则这是您要求的行为吗?

Same will now be printing out, which unless I am wrong is the behaviour you require?

这篇关于XML反序列化“标准化"行的结尾,如何制止它? (.网)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆