开发者

Show XML string in html SPAN tag

I have some string values in a dataset. In my composite control, in RenderContent I add dataset values to html table cells using span tags. It works great until I have an xml string in my dataset.

In my RenderControl I have code something like:

output.Write(@"<span id=""valueSpan{0}"" action=""edit"" type=""text"">{1}</span>", this.ID + row["ID"], row["Value"]);

row["Value"] c开发者_运维知识库ontains string value of:

<?xml version="1.0" encoding="utf-8"?><testdata>need to display this xml string in span</testdata>

The result I see is "need to display this xml string in span" not the XML data as "<?xml version="1.0" encoding="utf-8"?><testdata>need to display this xml string in span</testdata>". I think I need to let html know that this is just a value. But how???


You'll need to escape the < and > characters, transforming them to their corresponding character entities &lt; and &gt;. Use Server.HtmlEncode() to do this for you:

output.Write(
   @"<span id=""valueSpan{0}{1}"" action=""edit"" type=""text"">{2}</span>",
   this.ID, row["ID"], Server.HtmlEncode(row["Value"].ToString())
);

(Also, note that I removed the concatenation this.ID + row["ID"] in favor of a string.Format() style syntax.)


It seems you are using C#.
I think you should use HtmlEncode to encode the XML data so you are able to show it. http://msdn.microsoft.com/en-us/library/w3te6wfz.aspx


Have you tried escaping the <> characters before you add them to the string. (the escape codes are < and >

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜