Recently, I've been using XML a lot in my side projects. When binding XML to GridViews, Repeaters and DataLists, it's
very easy to set the element to display with an XPath command. For example, here's how to bind to DataList:
1<asp:DataList ID="dlBooks" runat="server">
2 <ItemTemplate>
3 <%# XPath("Title") %><br />
4 by <%# XPath("Author") %><br />
5 <br />
6 <i><%# XPath("BookText") %></i>
7 </ItemTemplate>
8</asp:DataList>
9
10
11protected void BindXML(string strXML)
12{
13 eNomInterface objAPI = null;
14 XmlDataSource xds = null;
15 try
16 {
17 xds = new XmlDataSource();
18 xds.Data = objAPI.Results.InnerXml;
19 xds.XPath = "*/Books";
20 dlBooks.DataSource = xds;
21 dlBooks.DataBind();
22 }
23 catch (Exception ex)
24 {
25 //Log exception
26 }
27 finally
28 {
29 if (xds != null)
30 {
31 xds.Dispose();
32 xds = null;
33 }
34 }
35}
But as I mentioned in a recent blog, I haven't memorized all of the XPath expressions. I know there aren't a ton, but who
has the time. When I'm writing code, I often reference other code to find the expression I need (what a pain to search
through code). Then when I test it out, I'm constantly reworking the expressions until I get what I want.
So that's when I decided to write this little application.
This application basically allows you to select or paste in source XML. Then you can enter in an XPath expression and
select the command to execute. After doing so, the results (or error) will display. You can also take your results and
load them into the source XML textbox with a button click...this allows you to easily test XPath expresssions on
results from previous queries. This is much better than experimenting in your code!
The XPath expression examples all came from MSDN. The application has links to the website and I also supplied the
example XML and XPath expressions. Also, I pulled in all of the MSDN examples so you can easily find an example of the
expression you're looking to execute.
Download
Application
Download
Source code
Comments
Leave a Comment