gpt4 book ai didi

c# - 不可打印字符的序列化

转载 作者:可可西里 更新时间:2023-11-01 08:06:45 26 4
gpt4 key购买 nike

以下代码;

var c = (char) 1;

var serializer = new XmlSerializer(typeof (string));

var writer = new StringWriter();
serializer.Serialize(writer, c.ToString());
var serialized = writer.ToString();

var dc = serializer.Deserialize(new StringReader(serialized));

在 .NET 4 中抛出此异常。

Invalid Operation Exception - There is an error in XML document (2, 12). '', hexadecimal value 0x01, is an invalid character. Line 2, position 12

我做错了什么吗?还是有合理的解决方法?

非常感谢!

最佳答案

有一个解决方法 explained here - 你可以使用 XmlReaderSettings.CheckCharacters忽略字符验证的选项:

XmlReader xr = XmlReader.Create(new StringReader(serialized),
new XmlReaderSettings { CheckCharacters = false });
var dc = (string)serializer.Deserialize(xr);

关于c# - 不可打印字符的序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6020441/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com