C# 中带有换行符的字符串元素的 XML 反序列化
我似乎无法弄清楚为什么这个测试没有通过
测试是:
给定以下 XML:
<?xml version="1.0" encoding="utf-8"?>
<foo>
<account>
1234567890
</account>
<deptCode>
ABCXYZ
</deptCode>
</foo>
和以下类:
class Foo {
[XmlElement(ElementName = "account", DataType = "normalizedString")]
string account;
[XmlElement(ElementName = "deptCode", DataType = "normalizedString"]
string deptCode;
}
当该 XML 反序列化时:
XmlSerializer serializer = new XmlSerializer(typeof(Foo));
Foo myFoo = (Foo) serializer.Deserialize(xmlReader);
我得到以下值:
Foo.account = "\r\n 1234567890 \r\n"
Foo.deptCode = "\r\n ABCXYZ \r\n"
而不是预期的
Foo.account = "1234567890"
Foo.deptCode = "ABCXYZ"
我该如何制作以便反序列化过程给我预期的结果?我认为 DataType="normalizedString"
可能会做到这一点,但它似乎没有效果,当我使用 XmlReaderSettings.IgnoreWhitespace
时,它只是带走了“\r ” 字符,留下“\n 1234567890”
I can't seem to figure out why this test doesn't pass
The test is:
given the following XML:
<?xml version="1.0" encoding="utf-8"?>
<foo>
<account>
1234567890
</account>
<deptCode>
ABCXYZ
</deptCode>
</foo>
and the following class:
class Foo {
[XmlElement(ElementName = "account", DataType = "normalizedString")]
string account;
[XmlElement(ElementName = "deptCode", DataType = "normalizedString"]
string deptCode;
}
when that XML is deserialized with:
XmlSerializer serializer = new XmlSerializer(typeof(Foo));
Foo myFoo = (Foo) serializer.Deserialize(xmlReader);
I get the following values:
Foo.account = "\r\n 1234567890 \r\n"
Foo.deptCode = "\r\n ABCXYZ \r\n"
instead of the expected
Foo.account = "1234567890"
Foo.deptCode = "ABCXYZ"
How can I make it so that the deserialization process gives me the expected results? I thought the DataType="normalizedString"
might do it, but it seems to have no effect, and when I use XmlReaderSettings.IgnoreWhitespace
, it just takes away the "\r" character, leaving me with "\n 1234567890"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来它正在按预期工作。来自
IgnoreWhitespace文档:
基本上,它的作用是保留(当设置为
false
时)元素之间的空格,例如:
和 <之间的换行符code>IgnoreWhitespace
设置为true
,则不会。为了实现你的目标,你必须进行程序化的修剪,正如基里尔提到的那样。当您考虑一下时,读者应该如何知道元素的纯字符串内容的空格(如您的示例中)是仅用于缩进目的还是实际内容?
有关忽略空格的更多阅读,您可能需要查看 这里和这里。
It seems it is working as intended. From
IgnoreWhitespace
documentation:Basically, what it does is preserves (when set to
false
) whitespaces in between elements such as:The newline between
<Foo>
and<bar>
will be returned by reader. SetIgnoreWhitespace
totrue
, and it won't.To achieve your goal you'll have to do programmatic trimming, as mentioned by Kirill. When you think about it, how is reader supposed to know whether whitespace of pure string content of element (as in your examples) is just for indenting purposes or actual content?
For more reading on ignoring whitespaces you may want to take a look here and here.
您可以创建自定义
XmlTextReader
类:You can create custom
XmlTextReader
class:尝试使用
XmlTextReader
进行反序列化,并将WhiteSpaceHandling
属性设置为WhiteSpaceHandling.None
和Normalization = true
Try using
XmlTextReader
for deserialization with theWhiteSpaceHandling
property set toWhiteSpaceHandling.None
andNormalization = true