如何在标签或文本框中显示完整的XML代码 [英] How do I display the full XML code in a label or textbox

查看:178
本文介绍了如何在标签或文本框中显示完整的XML代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当输出为:

 < <时,如何在标签或文本框中显示完整的XML代码/ span>  NewCode  >  
< Page >
< ; ref1 > 1 < / ref1 >
< ; ref2 > 2 < / ref2 >
< ; ref3 > 3 < / ref3 >
< / Page >
< Page >
< ref1 > 4 < / ref1 >
< ref2 > 5 < / ref2 >
< re f3 > 6 < / ref3 >
< / Page >
< >
< ref1 > 7 < / ref1 >
< ref2 > 8 < / ref2 >
< < span class =code-leadattribute> ref3
> 9 < / ref3 >
< < span class =code-leadattribute> / Page >
< / NewCode >



相反,它打印出来:

 1 2 3 4 5 6 7 8 9 





我尝试了什么:



每次我尝试使用括号中的代码<>没有显示。如何显示括号?

解决方案

根据您之前的问题,我将假设这是一个ASP.NET应用程序。



在这种情况下,您需要对数据进行HTML编码:

 YourLabel.Text = HttpUtility.HtmlEncode(yourXmlString); 



如果你想保留换行符,你还需要用< br> :

<预郎= C#> YourLabel.Text = HttpUtility.HtmlEncode(yourXmlString).Replace(<跨度类= 代码串> <跨度类= code-string> \ n, < br>) ;



注意:如果您将 放入文本框;控件将为您处理编码。但它只能在多行文本框中正确显示。


How do you display the full XML code in a Label or Textbox when the output is:

<NewCode>
     <Page>
         <ref1>1</ref1>
         <ref2>2</ref2>
         <ref3>3</ref3>
     </Page>
     <Page>
         <ref1>4</ref1>
         <ref2>5</ref2>
         <ref3>6</ref3>
     </Page>
     <Page>
         <ref1>7</ref1>
         <ref2>8</ref2>
         <ref3>9</ref3>
     </Page>
</NewCode>


Instead it is printing out:

1 2 3 4 5 6 7 8 9



What I have tried:

Every way I try the code in bracket "<>" is not shown. How do you show the brackets?

解决方案

Based on your previous questions, I'm going to assume this is an ASP.NET application.

In which case, you need to HTML-encode the data:

YourLabel.Text = HttpUtility.HtmlEncode(yourXmlString);


If you want to preserve the line-breaks, you'll also need to replace them with <br>:

YourLabel.Text = HttpUtility.HtmlEncode(yourXmlString).Replace("\n", "<br>");


NB: You don't need to encode it if you're putting it in a TextBox; the control will take care of the encoding for you. But it will only display properly in a multi-line textbox.


这篇关于如何在标签或文本框中显示完整的XML代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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