读取XML字符串与文本换行 [英] Read xml string into textbox with newline

查看:978
本文介绍了读取XML字符串与文本换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个成员地址,我从具有以下格式(假地址 - 该XML字段名为地址 - 未显示)的XML文件读取类:

I have a class with a member "Address" that I read from an XML file that has the following format(fake address - The XML field name is "Address" - not shown):

106-1190新月圣\r\\\
多伦多安大略省\r\\\
V2K 2Z6

我创建了一个多行文本框的形式来显示与接受,返回属性设置为true。

I have created a Multi-Line Textbox to display in a form with the Accepts-Return property set to true.

文本框中会显示新行字符以下不工作

The Textbox displays the following with the new-line characters not working

106-1190新月圣\r\\\
多伦多安大略省\r\\\
V2K 2Z6

我在网上找了各种解决方案,我已经尝试了以下内容:

I've looked online for various solutions and I have tried the following:

1)\\\
和\\\ \

1) \n and \\n

2)\\r\\\\

2) \\r\\n

它的工作原理,如果我加入物业直接像这样:

It works if I add to the property directly like so:

Address.Text = "106-1190 Crescent St.\r\nToronto Ont\r\nV2K 2Z6";



但是,如果我从一个XML文件加载一个字符串变量,并尝试将其分配到的Text属性:

But if I load a string variable from an XML file and try to assign it to the Text property:

Address.Text = Employee.Address;



这是行不通的。

It does not work.

我设置观察点检查直接分配和变量赋值昂之间的区别了这种差异:

I set watch points to check the difference between the direct assignment and the variable assignment ang got this difference:

直接: 106-1190新月圣\r\\ \
多伦多安大略省\r\\\
V2K 2Z6

变量: 106-1190新月圣\\r\\\ \
多伦多安大略省\\r\\\\
V2K 2Z6

所以某处代码改变反斜线一个双反斜线从转换XML字符串的变量。

so somewhere the code is changing the backslash to a double backslash in converting from the XML string to the variable.

我真的不希望有我的解析XML字符串,删除多余的斜线....
怎么办我代表我的字符串数据在XML文件中,这样对我的换行符正确显示它被转换成一个适当的字符串变量只用单斜线,以

I don't really want to have to parse my XML strings to remove the extra slash.... How do I represent my string data in an XML file such that it gets converted to a proper string variable with just the single slash, in order for my line breaks to show up properly.

先谢谢了。

推荐答案

TextBox控件不解释转义而且也不XML。所以这只是人物\ R和n。你需要处理的字符串替换字符串@\r\\\
为\r\\\

The textbox control doesn't interpret escapes and neither does XML. so this is just the characters \ r and n. You need to process the string, substituting the string @"\r\n" for "\r\n".

Address.Text = Employee.Address.Replace(@"\r\n", "\r\n");

这是等同于本

Address.Text = Employee.Address.Replace("\\r\\n", "\r\n");

由于@防止串\的解释。

because @ prevents interpreting of \ in the string.

编辑:基于一个评论,这是更好的形式,将其转换为\\\
在UNIX和Windows \r\\\

edit: based on a comment, this is even better form, it translates to \n on unix, and \r\n on Windows.

Address.Text = Employee.Address.Replace("\\r\\n", Environment.NewLine);

这篇关于读取XML字符串与文本换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆