Working with Html in XmlDataSource XML Data Source Files
Today I was working with some XmlDataSource controls to populate some Repeaters and other databound controls and ran into a problem – the XmlDataSource doesn’t show any tags that are found within the XML. For instance, if I have this XML file:
<customers>
<customer>Widgets R Us <img src=”/images/widgetslogo.gif” /></customer>
<customer>Acme Inc.</customer>
</customers>
The XmlDataSource would not show the <img /> tag, only the text. I did a bit of searching and didn’t find an easy way to get this to work out of the box, so I ended up using syntax like this:
<%# ParseLogo(XPath(“customer”)) %>
Where ParseLogo() might look like this:
protected string ParseLogo(object content)
{
string myContent = content.ToString();
string logoImg= "<img src=\"" + ResolveUrl("~/images/logo.gif") + "\" width=\"18\" height=\"15\" class=\"logo\" />";
return myContent.Replace("[LOGO]", logoImg);
}
This works but it seems like a hack. If anybody knows of a simple way to enable getting the InnerHtml (as opposed to InnerText) of the data node using an XmlDataSource, please let me know.