views:

360

answers:

3

I'm trying to list vacancies in a asp:GridView using an XML_feed through an asp:XmlDataSource. This should be kinda simple, but I'm missing something with my XPath-expressions..

This is a short example of what I'm trying to achieve (list title of vacancies):

<asp:XmlDataSource ID="XMLsource" DataFile="http://demo.easycruit.com/export/xml/vacancy/list.xml" XPath="VacancyList/Vacancy" runat="server"/>
<asp:GridView DataSourceID="XMLsource" AutoGenerateColumns="False" runat="server">      
<Columns>
        <asp:TemplateField>
            <HeaderTemplate>Title</HeaderTemplate>
            <ItemTemplate><%# XPath( "Version/Title" ) %></ItemTemplate>             
 </asp:TemplateField>         
</Columns>
</asp:GridView>

Both controllers have DataBind() in codebehind. So, if someone knows why this doen't work... :)

+1  A: 

If you have a look at your XML (just the first few lines):

<VacancyList generated="2009-08-04T18:43:17" 
             xmlns="urn:EasyCruit" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xsi:schemaLocation="http://www.easycruit.com/dtd/vacancy-list.xsd"&gt;
  <Vacancy id="82034" date_start="2007-04-17" date_end="2009-12-22" 
           reference_number="CDP-GR3">
    <Versions>
      <Version language="fr">
        <Title>Chef de produit (H/F)</Title>
        <TitleHeading/>

You bind your datagrid to the list of <Vacancy> elements - fine so far.

But then in your grid, you reference : <%# XPath( "Version/Title" ) %>

This won't work, since the <Vacancy> does not have a '/ inside it - these elements are within a ` collection.....

So what you need to reference in your ItemTemplate would be:

<%# XPath( "Versions/Version[@language='fr']/Title" ) %>

That should work.

UPDATE:
there appears to be an additional problem with the ASP.NET 2.0 XmlDataSource not being able to handle default XML namespaces :-(

That's this line here in your XML:

<VacancyList ........
             xmlns="urn:EasyCruit"

See this blog post here on the topic: http://jasonf-blog.blogspot.com/2006/08/xmldatasource-xpath-workaround-for.html

There's really two things you could do to fix this:

  • use an XSLT transformation to strip out the default namespace so that the XmlDataSource can handle the data
  • load the data from the URL in code, and bind it to the GridView in your code-behind

UPDATE 2: The method of stripping out the XML namespaces seems to work quite nicely - Bill Evjen proposes this method here.

If you save his XSLT file in the post to a file called "StripNamespaces.xslt" in the web site project, you should get your data if you change the asp:XmlDataSource to be:

<asp:XmlDataSource ID="XMLsource" runat="server"
            DataFile="http://demo.easycruit.com/export/xml/vacancy/list.xml" 
            TransformFile="~/StripNamespaces.xslt"
            XPath="VacancyList/Vacancy" />

Note the new "TransformFile" setting - this must reference that XSLT file. With this in place, I am now getting data displayed in the GridView.

Marc

marc_s
A: 

I do agree, it should work, but it doesn't!! This should have been a rather simple task, but as soon as I enter any expression in the XPath-parameter in the datasource I get no "hits". I just can't seem to get my head round and find out what's wrong..

Chr

A: 

Thanks! it worked great!! I tried to set your answer as accepted, but i wasn't registered, so.. :(

chr :)