0

我的数据实体包含一个字典,但 XmlSerializer 不支持它们开箱即用。所以我决定使用 DataContractSerializer。问题是我无法让它按照我的需要运行。

我从以下代码开始:

public static string SerializeObject<T>(T serialisable)
{
    var serializer = new DataContractSerializer(serialisable.GetType());
    using (var writer = new StringWriter())
    using (var stm = new XmlTextWriter(writer))
    {
        serializer.WriteObject(stm, serialisable);
        return writer.ToString();
    }
}

它似乎工作正常,直到我注意到如果我将“\r\n”放在一个字符串中,它不会被序列化为 XML 实体。根据我使用 XmlSerializer 的经验,我知道我可以使用 NewLineHandling = NewLineHandling.Entitize 设置 XmlWriterSettings。所以我将我的代码转换为以下内容:

public static string SerializeObject<T>(T serialisable)
{
    var serializer = new DataContractSerializer(serialisable.GetType());
    using (var writer = new StringWriter())
    {
        using (var stm = XmlWriter.Create(writer,
            new XmlWriterSettings()
            {
                NewLineHandling = NewLineHandling.Entitize
            }))
        {
            serializer.WriteObject(stm, serialisable);
            return writer.ToString();
        }
    }
}

现在的问题是我得到一个空字符串。没有例外,没有 - 只是一个空字符串。stm 变量保存 XmlWellFormedWriter。也许 DataContractSerializer 不支持它?

然后我尝试执行 XmlTextWriter 如下:

public static string SerializeObject<T>(T serialisable)
{
    var serializer = new DataContractSerializer(serialisable.GetType());
    using (var writer = new StringWriter())
    using (var stm = XmlWriter.Create(new XmlTextWriter(writer),
        new XmlWriterSettings()
        {
            NewLineHandling = NewLineHandling.Entitize
        }))
    {
        serializer.WriteObject(stm, serialisable);
        return writer.ToString();
    }
}

这让我回到了开始的地方——我取回了 XML 字符串,但“\r\n”字符串又没有被转换为实体。

如何使 DataContractSerializer 实体化换行符并将 XML 作为字符串返回?

4

2 回答 2

2

我知道这是一个相当古老的线程,但我偶然发现它正在寻找答案,并认为我会回答我发现的问题。

\n 没有被实体化的原因是因为它们在文本节点值中。如果 \n 字符在属性中,则序列化程序只会实体化它们。

这是我发现在每个 NewLineHandling 值中会发生的情况

文本节点

NewLineHandling.Replace (Default) 
\r \n \r\n all go to \r\n
\t remains as \t

NewLineHandling.Entitize
\r\n goes to &#D;
\n remains as \n
\r goes to &#D;
\t remains as \t

NewLineHandling.None
\r remains \r
\r\n remains \n
\r\n remains \r\n
\t remains as \t

属性

NewLineHandling.Replace (Default) 
\r\n goes to &#D;&#A;
\n goes to &#A;
\r goes to &#D;
\t remains &#9;

NewLineHandling.Entitize
\r\n goes to &#D;&#A;
\n goes to &#A;
\r goes to &#D;
\t remains &#9;

NewLineHandling.None
\r remains \r
\r\n remains as \n
\r\n remains as \r\n
\t remains as \t
于 2014-04-01T22:56:01.920 回答
0

看来,问题主要是因为处理 XmlWriter 的工作方式 - 如果我使用 XmlWriter.Create 创建它,它在关闭之前不会刷新,因此 StringWriter 是空的。奇怪的是 - 如果我用新的 XmlTextWriter 创建它,它会以某种方式将其内容刷新到 StringWriter,所以我的初始方法工作得很好。

这次我只需要重新排列一行代码:

    public static string SerializeObject<T>(T serialisable)
    {
        var serializer = new DataContractSerializer(serialisable.GetType());
        using (var writer = new StringWriter())
        {
            using (var stm = XmlWriter.Create(writer,
                new XmlWriterSettings()
                {
                    NewLineHandling = NewLineHandling.Entitize,
                    Encoding = UTF8Encoding.UTF8
                }))
            {
                serializer.WriteObject(stm, serialisable);
                // <- previously writer.ToString() was here and I got an empty string
            }     

            return writer.ToString();
        }
    }

现在 "\r" 字符被正确编码为&#xD;,但 "\n" 不是。而且编码还是utf-16,虽然我设置为UTF8。我想,那是另一个问题。

于 2013-01-30T13:09:23.680 回答