0

I'm using an XmlDataSource in my ASP.NET application as a way to prototype schema and data before actually implementing on SQL Server database. So far I am able to bind .NET controls to the XmlDataSource and corresponding XML file in the expected manner, but I am unable to filter out only the desired records without hardcoding it in the XPath. Using the following example, I'd like to only retrieve tasks from User_ID 1.

In SqlDataSource I would use SelectParameter, but I am not finding any documentation if this is possible with an XmlDataSource.

XML:

<?xml version="1.0" encoding="utf-8" ?>
<Tasks>
  <Task>
    <Task_ID>1</ID>
    <User_ID>1</User_ID>    
    <Title>My Task</Title>
  </Task>
  <Task>
    <TaskID>2</Task_ID>
    <User_ID>2</User_ID>
    <Title>Another Task</Title>
  </Task>
</Tasks>

ASPX:

<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~\App_Data\tasks.xml" XPath="//Tasks/Task">
</asp:XmlDataSource>
atjoedonahue
  • 476
  • 6
  • 19
  • 1
    I'd avoid this whole thing entirely. Avoid `XmlDataSource` and `SqlDataSource`. Switch to [ObjectDataSource](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource(v=vs.110).aspx) and tie that into a repository pattern that abstracts away how you interact with the data storage. – mason Mar 25 '17 at 12:50
  • I haven't used [XmlDataSource](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.xmldatasource(v=vs.110).aspx) a lot but the documentation doesn't mention anything about select parameters. It mentions using XPath filtering. Which means your UI logic needs to be adjusted to handling filtering differently, which means it won't be directly applicable to when you do actually move to using a relational database. That's why it's important to abstract away the storage logic from the presentation logic. – mason Mar 25 '17 at 13:34

0 Answers0