C# 中带有换行符的字符串元素的 XML 反序列化

发布于 2024-12-11 13:23:47 字数 1030 浏览 0 评论 0原文

我似乎无法弄清楚为什么这个测试没有通过

测试是:

给定以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

全部不再 2024-12-18 13:23:47

看起来它正在按预期工作。来自 IgnoreWhitespace文档:

不被认为重要的空白包括空格、制表符和用于分隔标记以提高可读性的空行。

基本上,它的作用是保留(当设置为false时)元素之间的空格,例如:

<Foo>

<bar>Text</bar>
</Foo>

和 <之间的换行符code>将由阅读器返回。将 IgnoreWhitespace 设置为 true,则不会。

为了实现你的目标,你必须进行程序化的修剪,正如基里尔提到的那样。当您考虑一下时,读者应该如何知道元素的纯字符串内容的空格(如您的示例中)是仅用于缩进目的还是实际内容?

有关忽略空格的更多阅读,您可能需要查看 这里这里

It seems it is working as intended. From IgnoreWhitespace documentation:

White space that is not considered to be significant includes spaces, tabs, and blank lines used to set apart the markup for greater readability.

Basically, what it does is preserves (when set to false) whitespaces in between elements such as:

<Foo>

<bar>Text</bar>
</Foo>

The newline between <Foo> and <bar> will be returned by reader. Set IgnoreWhitespace to true, 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.

无远思近则忧 2024-12-18 13:23:47

您可以创建自定义 XmlTextReader 类:

public class CustomXmlTextReader : XmlTextReader
{
    public CustomXmlTextReader(Stream stream) : base(stream) { }

    public override string ReadString()
    {
        return base.ReadString().Trim();
    }
}

You can create custom XmlTextReader class:

public class CustomXmlTextReader : XmlTextReader
{
    public CustomXmlTextReader(Stream stream) : base(stream) { }

    public override string ReadString()
    {
        return base.ReadString().Trim();
    }
}
亣腦蒛氧 2024-12-18 13:23:47

尝试使用 XmlTextReader 进行反序列化,并将 WhiteSpaceHandling 属性设置为 WhiteSpaceHandling.NoneNormalization = true

Try using XmlTextReader for deserialization with the WhiteSpaceHandling property set to WhiteSpaceHandling.None and Normalization = true

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文