Binding the XmlDataSource to XML Nodes Inner HTML CDATA Values

Quick post today about something I should've found obvious. Posting here so I never forget.

When binding in ASP.NET with the XmlDataSource, you can easily use Eval("") to bind to the node properties. However, I couldn't for the life of me figure out how to bind the actual node inner value.

For example if I have an xml file like this:


<images>
    <image src="imagea.jpg" alt="Alt Text A">
        <![CDATA[<p>Caption A</p>]]>
    </image>
    <image src="imageb.jpg" alt="Alt Text B">
        <![CDATA\[<p>Caption B</p>]]>
    </image>
</images>

I could easily bind the src and alt properties with Eval(""):

<asp:Repeater ID="ImgRepeater" runat="server" DataSourceID>="RotatorSource">
    <ItemTemplate>
        <img src="<%# Eval("src") %>" alt="<%# Eval("alt") %>" />
        <p><!-- Caption ? --></p>
    </ItemTemplate>
</asp:Repeater>
<asp:XmlDataSource ID>="RotatorSource" runat="server" DataFile="~/Images/rotator/Rotator.xml" XPath="images/image" />

I tried several different things like Eval("image") and even tried using the Container.DataItem value with no luck.

I also tried different values for XPath, like XPath("image"). This did work, but it would only show the first value for all of the images. XPath("../images") and other variations were of no help.

I wasn't even sure how to search for this. I hardly use XML so I wasn't sure what terminology to even search for...

Fortunately, before I gave up and went to the bar, I found this thread: XmlDataSource and embeded HTML within your XML data, which pointed me to this helpful solution:

<%# XPath(".") %>

Now it all works! And to anyone else who might have been in the same boat and landed here for a solution: Doesn't XML suck?

Enjoyed this post and/or found it useful?
SelArom Dot Net Profile Image
SelAromDotNet

Josh loves all things Microsoft and Windows, and develops solutions for Web, Desktop and Mobile using the .NET Framework, Azure, UWP and everything else in the Microsoft Stack.

His other passion is music, and in his spare time Josh spins and produces electronic music under the name DJ SelArom.



Scroll to top