简体   繁体   English

让C#尊重\\ n从Web服务返回一个字符串?

[英]Get C# to respect \n returned in a string from a web service?

I have a messaging system that is a .NET 2 ASMX web service, very basic system. 我有一个消息传递系统,它是一个.NET 2 ASMX Web服务,非常基本的系统。 I push messages and would like to have newline symbols in the message so it's formatted on the receiving end. 我推送消息,并希望在消息中有换行符号,以便在接收端格式化。 For example, I'd like to send a string such as: "Hello\\n\\nMy Name Is..." and have it have two line breaks. 例如,我想发送一个字符串,例如:“Hello \\ n \\ n我的名字是......”并让它有两个换行符。 When the receiver reads the text it's actually outputting the \\n's in the text. 当接收器读取文本时,它实际上输出文本中的\\ n。 How can I get the \\n's to be interpreted as if I was writing it in C#? 我怎样才能将\\ n解释为好像我是用C#编写的? Or \\t etc. 或\\等

Thank you. 谢谢。

You should compose the message using Environment.NewLine to insert the character(s) used to represent a newline on your system. 您应该使用Environment.NewLine编写消息,以插入用于表示系统换行符的字符。

See here for references 请参阅此处以获取参考

However using Environment.NewLine has some problems. 但是使用Environment.NewLine有一些问题。
For example, your message should be written in this way 例如,您的消息应该以这种方式编写

string msg = "Hello" + Environment.NewLine + Environment.NewLine + "My Name Is...";

a bit cumbersome to use from a programmer point of view. 从程序员的角度来看,使用起来有点麻烦。

Then you could write an extension method for the string class which takes your message and insert at the place of a placeholder the Environment.NewLine chars. 然后你可以为字符串类编写一个扩展方法,它接收你的消息并在占位符的位置插入Environment.NewLine字符。

This example use the | 这个例子使用| (pipe) character as placeholder for the newline pos. (管道)字符作为换行符pos的占位符。

public static string InsertLineBreaks(this string inMsg)
{
    Strinbuilder sb = new StringBuilder(inMsg);
    sb.Replace("|", Environment.NewLine);
    return sb.ToString();
}

and you can call this extension in this way 你可以用这种方式调用这个扩展名

string msg = "Hello||My Name Is...".InsertLineBreaks();

The web service is likely sending the message back with carriage returns listed as "%0D". Web服务可能会发回消息,并将回车列为“%0D”。 What I have done in my programs is use: 我在我的程序中所做的是使用:

str.Replace("%0D", Environment.NewLine);

That seems to work for me. 这似乎对我有用。

I'm sure you could also use: 我相信你也可以使用:

str.Replace("%0D", "\n");

You have to replace the \\n with something that is interpreted as a new line by the browser, ie <br/> . 您必须将\\n替换为浏览器解释为新行的内容,即<br/> I suppose you're using it as an HTML string. 我想你将它用作HTML字符串。

Besides you need to get sure that it isn't HtmlEncoded, ie, that your <br/> isn't converted into &lt;br/&gt; 除此之外,您还需要得到肯定它不是HtmlEncoded,即你的<br/>不转化为&lt;br/&gt; . This shouldn't happend if you're using JSON serialization. 如果您正在使用JSON序列化,则不应该发生这种情况。 but will happen if you serialize it as XML and isn't properly decoded in client side. 但如果您将其序列化为XML并且未在客户端进行正确解码,则会发生这种情况。

用这个:

string returnString = "Hello" + Environment.NewLine+ "My Name Is..";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM