Welcome to AspAdvice Sign in | Join | Help

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.

Published Monday, July 24, 2006 11:50 PM by ssmith
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Working with Html in XmlDataSource XML Data Source Files

I doubt there is that easy way. You already picked it.(TemplateControl.)XPathSelect would return IEnumerable (it's an ArrayList in practise) which contains the XmlElements returned by the XPath expression. Playing with them would probably work, but you'd have 2 or 3 times more code than now.

(TemplateControl.)XPath itself returns the value of the node when it basically is node's value plus values of child nodes concatenated (as string, yes).

I suppose LinQ stuff will answer this quite well when you can template the result XML (now it's either XSLT or doing it in code with DOM or XPath classes)
Thursday, July 27, 2006 12:04 PM by joteke

# re: Working with Html in XmlDataSource XML Data Source Files

your xml file is invalid in the way you are trying to use it ( the img tag is the child element of the customer tag not the inner text ).

it should read

<customers>

 <customer><![CDATA[Widgets R Us <img src=”/images/widgetslogo.gif” />]]></customer>

 <customer>Acme Inc.</customer>

</customers>

Tuesday, September 26, 2006 7:49 AM by frazerm

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below