开发者

C# multiline string with html

开发者 https://www.devze.com 2023-02-14 10:49 出处:网络
Is it possible to have a multiline C# string that contains html? The following works fine: string output = @\"Qiuck

Is it possible to have a multiline C# string that contains html?

The following works fine:

        string output = @"Qiuck 
                        brown
                        fox
                        jumps
                        over
                        the
                        lazy 
                        log";

But this does not work:

            string output = @"&开发者_高级运维lt;object>
                        <node>
                        <param value=\"test\" />
                        </node>
                        </object>
                        ";

However this similar example does work, I have just taken out the attribute on param:

            string output = @"<object>
                        <node>
                        <param />
                        </node>
                        </object>
                        ";

Any suggestions on the best way to package html into a string variable? If it is not possible I am assuming the next best method is just to read from a file? Any other ideas?

The problem with example 2 seems to be the escaped quotes.


Use double quotes instead of escaping them.

    string output = @"<object>
                    <node>
                    <param value=""test"" />
                    </node>
                    </object>
                    ";


Use "" instead of \". It will still output ". When you're doing literal strings, the escape character isn't processed:

string output = @"<object>
                      <node>
                          <param value=""test"" />
                      </node>
                  </object>
                  ";


Use single quotes in the param.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号