2

With ASP.NET 3.5 I can easily bind to an XML file by using an XmlDataSource.

How can I bind to an XML string instead of a file?

2 Answers 2

4

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>
Sign up to request clarification or add additional context in comments.

Comments

1

From the XmlDataSource docs here:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.