开发者

Displaying HTML encoded styling in WPF

I get a string from database that includes a HTML styling (not sure what it's called) and I wan't to display it in my WPF app.

I can decode in the ViewModel so I can use the .net library.

I tried using WebUtility.HtmlDecode(string) method but it doesn't do anything (and probably isn't supposed to).

this is what the string might look like:

<span style="background-color: rgb(255, 102, 0);" class="Apple-style-span">This</span><div>开发者_Python百科;<span style="background-color: rgb(255, 102, 0);" class="Apple-style-span">is</span></div><div><span style="background-color: rgb(255, 102, 0);" class="Apple-style-span">a</span></div><div><span style="background-color: rgb(255, 102, 0);" class="Apple-style-span">weird</span></div><div><span style="background-color: rgb(255, 102, 0);" class="Apple-style-span">look</span></div><div></div>

Getting just the text I could do with a little XML coding but I'd rather use something that already exists. Being able to get the styles as well would be great but I'm not looking towards including the WinForms browser thingy.

Notice I only need to display the data, not edit it.


This has nothing to do with encoding, this is about conversion. If you don't want to use the WebBrowser control you need to convert from HTML to flow content which can be displayed in a FlowDocument. You can either write the necessary code yourself or see if this converter which converts both ways (or any other existing converter for that matter) fits your needs.


You don't necessarily need a proper HTML page to make the WebBrowser display it, you can just do this:

string htmlString = "<span style=\"background-color: rgb(255, 102, 0);\" class=\"Apple-style-span\">This</span><div><span style=\"background-color: rgb(255, 102, 0);\" class=\"Apple-style-span\">is</span></div><div><span style=\"background-color: rgb(255, 102, 0);\" class=\"Apple-style-span\">a</span></div><div><span style=\"background-color: rgb(255, 102, 0);\" class=\"Apple-style-span\">weird</span></div><div><span style=\"background-color: rgb(255, 102, 0);\" class=\"Apple-style-span\">look</span></div><div></div>";
wb.NavigateToString(htmlString);

But ideally you can wrap your incomming string in a proper html-frame first, which should be rather easy to do.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜