当前位置: 动力学知识库 > 问答 > 编程问答 >

html - How can I take an xml string and display it on my page similiar to how StackOverflow does it with 'insert code'?

问题描述:

I'm using the DataContractSerializer to convert and object returned from a WCF call to xml. The client would like to see that xml string in a webpage. If I output the string directly to a label, the browser strips out the angle brackets obviously. My question is how can I do something similar to StackOverflow? Are they doing a find & replace to replace angle brackets with their html entities? I see they are doing a code tag inside a pre tag and then making spans with the appropriate class. Is there an existing utility out there I can use to do this instead of writing some kind of parsing routine. I'm sure something free must be out there. If anyone can direct to the right place or some code that can easily accomplish this, I would greatly appreciate it. I apologize if this is more of a meta.stackoverflow question. Thanks for any tips.

网友答案:

The basic answer is that to get HTML displayed as typed, special characters and all, you need to replace the special characters (<, > etc.), with their escaped equivalents (&gt;, &lt; etc.). Beyond that if you want syntax colouring you'll have to parse the input to identify the keywords etc.

A full list of the special characters and their escape codes can be found here, but this is just one site of many.

网友答案:

you're talking about "pretty print".. if you want to diplay source code you could use this link 16 Free Javascript Code Syntax Highlighters For Better Programming

But if you want to display only xml.. there are some functions on the web that could help you with that, like this one: XML PHP Pretty Printer

and dont forget the special characters =)

good luck

分享给朋友:
您可能感兴趣的文章:
随机阅读: