3

I am using ASP.Net 2.0 and trying to display transformed xml data using GridView and XMLDataSource at run time.

Here is my xml data (Input.xml):

<catalog>
<cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
</cd>
<cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
</cd>
<cd>
    <title>Greatest Hits</title>
    <artist>Dolly Parton</artist>
    <country>USA</country>
    <company>RCA</company>
    <price>9.90</price>
    <year>1982</year>
</cd>
<cd>
    <title>Still got the blues</title>
    <artist>Gary Moore</artist>
    <country>UK</country>
    <company>Virgin records</company>
    <price>10.20</price>
    <year>1990</year>
</cd>
</catalog>

And here is the transform (Transform.xslt):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
    <CDCatalog>
        <xsl:apply-templates/>
    </CDCatalog>
</xsl:template>

<xsl:template match="cd">
  <CD>
  <xsl:apply-templates select="*">
        </xsl:apply-templates>
  </CD>
</xsl:template>


<xsl:template match="catalog/cd/*">
            <xsl:attribute name="{local-name()}">
        <xsl:value-of select="."/>
    </xsl:attribute>
</xsl:template>

</xsl:stylesheet>

And for your reference this would be the transformed xml:

<CDCatalog>
<CD title="Empire Burlesque" artist="Bob Dylan" country="USA" company="Columbia" price="10.90" year="1985"/>
<CD title="Hide your heart" artist="Bonnie Tyler" country="UK" company="CBS Records" price="9.90" year="1988"/>
<CD title="Greatest Hits" artist="Dolly Parton" country="USA" company="RCA" price="9.90" year="1982"/>
<CD title="Still got the blues" artist="Gary Moore" country="UK" company="Virgin records" price="10.20" year="1990"/>
</CDCatalog>

Now this what I am doing in my C# code (GridView1 is created on .aspx page):

        XmlDataSource xmlDS = new XmlDataSource();
        xmlDS.EnableCaching = false;
        xmlDS.DataFile = "~/Input.xml";
        xmlDS.TransformFile = "~/Transform.xslt";
        xmlDS.XPath = "/CDCatalog/CD";
        GridView1.DataSourceID = xmlDS.ID;
        GridView1.DataBind();
        GridView1.Visible = true;

I do not see any data in the GridView. I looked up quite a bit on the internet but they generally talk about doing this sort of thing at design time in the aspx page but not at run time with in the code. I am not sure if this is a limitation on GridView/XMLDataSource binding or if I am doing something wrong. I appreciate any help on this. Thanks, Srinivas

0

1 Answer 1

2

Simply change this line:

GridView1.DataSourceID = xmlDS.ID;

to this:

GridView1.DataSource = xmlDS;

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

You can also remove GridView1.Visible = true; it's visible by default, unless you alter the visibility somewhere else.
Using DataSource instead of DataSourceID works. When we create a new instance of XMLDataSource the ID property is set to blank and probably that's why setting xmlDS.ID wasn't working. I couldn't find the source now but I think I read some where that if you set the DataSource itself you may lose the sorting ability of the grid. Do you think that's truly the case? I am setting your reply as answer since its answers my initial question though I am not sure if this is what I will end up using. The option I am using is to put a blank data source on asp.net design page and use it by ID.

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.