views:

996

answers:

2

With ASP.NET 3.5 I can easily bind to an xml file by using an XmlDataSource. How do I bind to an xml string instead of a file?

+1  A: 

From the XmlDataSource docs here:

XML data can also be stored directly by the data source control in string form using the Data property.

Dan Blanchard
+2  A: 

Use the XmlDataSource.Data property.

XmlDataSource dataSource    = new XmlDataSource();
dataSource.Data             = "<root><element>Item #1</element><element>Item #2</element></root>";
dataSource.XPath            = "root/element";
dataSource.DataBind();

Alternately, you could specify the data declaratively:

<asp:xmldatasource 
  id="XmlDataSource1" 
  runat="server" 
>
  <data>
    <Books>
      <LanguageBooks>
        <Book Title="Pure JavaScript" Author="Wyke, Gilliam, and Ting"/>
        <Book Title="Effective C++ Second Edition" Author="Scott Meyers"/>
        <Book Title="Assembly Language Step-By-Step" Author="Jeff Duntemann"/>
        <Book Title="Oracle PL/SQL" Author="Steven Feuerstein"/>
      </LanguageBooks>

      <SecurityBooks>
        <Book Title="Counter Hack" Author="Ed Skoudis"/>
      </SecurityBooks>

    </Books>
  </data>
</asp:xmldatasource>
Oppositional