如何将多行文本框的内容保存和加载到 XML?

发布于 2024-12-06 20:12:31 字数 974 浏览 0 评论 0原文

我有一个多行文本框,我使用数据绑定将其 Text 属性绑定到字符串。这似乎有效,但是从 XML 加载字符串后,返回值(新行)就会丢失。当我检查 XML 时,返回值就在那里,但是一旦从 XML 加载字符串,它们就会丢失。有谁知道为什么会发生这种情况以及如何正确执行此操作。

(我不一定要使用多行文本框或字符串属性进行绑定,我只是一个可维护的(最好是优雅的)解决方案。)

编辑:基本上,我使用 XmlSerializer 类:

加载:

using (StreamReader streamReader = new StreamReader(fileName))
{
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
    return (T)xmlSerializer.Deserialize(streamReader);
}

保存:

using (StreamWriter streamWriter = new StreamWriter(fileName))
{
   Type t = typeof(T);
   XmlSerializer xmlSerializer = new XmlSerializer(t);
   xmlSerializer.Serialize(streamWriter, data);
}

当查看内部时XML,它保存多行文本框数据,如下所示:

<OverriddenComponent>
   <overrideInformation>
          <Comments>first rule
second rule
third rule</Comments>
    </overrideInformation>
</OverriddenComponent>

但加载数据后,这些中断不再显示。

I have a multiline textbox, and I use databinding to bind its Text property to a string. This appears to work, but upon loading the string from XML, the returns (new lines) get lost. When I inspect the XML, the returns are there, but once the string is loaded from XML they are lost. Does anybody know why this is happening and how to do this right.

(I am not bound to use either a multiline textbox, or a string property for binding, I just a maintainable, (and preferably elegant) solution. )

Edit: Basically, I use the XmlSerializer class:

loading:

using (StreamReader streamReader = new StreamReader(fileName))
{
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
    return (T)xmlSerializer.Deserialize(streamReader);
}

saving:

using (StreamWriter streamWriter = new StreamWriter(fileName))
{
   Type t = typeof(T);
   XmlSerializer xmlSerializer = new XmlSerializer(t);
   xmlSerializer.Serialize(streamWriter, data);
}

When looking inside the XML, it saves multiline textbox data like this:

<OverriddenComponent>
   <overrideInformation>
          <Comments>first rule
second rule
third rule</Comments>
    </overrideInformation>
</OverriddenComponent>

But those breaks no longer get displayed after the data is loaded.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

梦与时光遇 2024-12-13 20:12:31

新线路的实际代码是什么? 0x0A 或 0x0D 或两者?我之前偶然发现了类似的问题。字符串中的字符“丢失”,因为文本框自行“转换”了它们(或者不理解它们)。基本上,您的 xml 文件可能以一种方式编码,而您的文本框使用其他编码,或者在读取或写入文件本身期间丢失(您的字符串也可能在读取/写入文件期间“混乱”) )。因此,您的字符串可能在您不知情的情况下在 3 个地方被篡改:

  1. 在写入文件期间(注意您使用的编码)
  2. 在从文件读取期间
  3. 在文本框中显示字符串时。

我的建议是,您应该将从文件中读取的文本分配给另一个字符串(未绑定),然后再将其分配给绑定的字符串,并使用调试器检查它如何更改。这个 http://home2.paulschou.net/tools/xlate/ 是一个有用的工具检查字符串中到底是什么。

当我在应用程序中遇到这个问题时,我最终使用字符的二进制/十六进制值来写入/读取,然后在需要显示时将它们转换回来。但我不得不使用很多奇怪的 ASCII 码。也许有一个更简单的解决方案适合您。

编辑:或者它可能只是一些与 xml 相关的东西。也许您应该在将其写入 xml 时使用其他字符来替换换行符?

What are the actual codes for new lines ? 0x0A or 0x0D or both? I stumbled on a similar problem before. The characters from a string "got lost" because textbox "converted" them on its own (or didn't understand them). Basicly, your xml file may be encoded one way, and your textbox uses other encoding, or it is lost during reading from, or writing to, the file itself (your string may be "messed up" also during reading from/writing to file). So there are 3 places your string may be tampered with, without your knowledge:

  1. During writing to the file (take notice what encoding you use)
  2. During reading from the file
  3. When displaying your string in textbox.

My advice is that you should assign the text that you read from the file to another string (not bound) before you assign it to the bound one and use a debugger to check how it changes. This http://home2.paulschou.net/tools/xlate/ is a useful tool to check what exactly is in your strings.

When I encountered this problem in my application, I ended up using binary/hex values of characters to write/read, and then converting them back when I needed to display. But I had to use a lot of strange ASCII codes. Maybe there's an easier solution for you out there.

EDIT: or it may be just some xml-related thing. Maybe you should use some other character to replace line break when writing it to xml?

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