3
我正在使用ASP.Net 2.0並試圖在運行時使用GridView和XMLDataSource顯示轉換後的xml數據。在運行時ASP.Net GridView和XMLDataSource綁定
這裏是我的XML數據(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>
這裏是變換(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>
,謹供參考,這將是轉換的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>
現在,這個我在做什麼,我的C#代碼(GridView1時的.aspx頁面上創建):
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;
我在GridView中看不到任何數據。我在互聯網上查了很多,但他們通常會在設計時在aspx頁面上討論這類事情,而不是在運行時在代碼中進行。我不確定這是否是對GridView/XMLDataSource綁定的限制,或者如果我做錯了什麼。我很感激這方面的幫助。 謝謝,SRINIVAS
您還可以刪除GridView1.Visible = TRUE;它在默認情況下是可見的,除非您在其他地方更改可見性。 – 2012-01-04 05:46:54
使用DataSource而不是DataSourceID工作。當我們創建XMLDataSource的新實例時,ID屬性設置爲空,可能這就是設置xmlDS.ID不起作用的原因。我現在找不到源代碼,但我想我會在某些地方閱讀,如果您設置了DataSource本身,您可能會失去網格的排序能力。你認爲這是真的嗎?我將你的答案設置爲答案,因爲它的答案是我最初的問題,儘管我不確定這是我最終會使用的答案。我正在使用的選項是在asp.net設計頁面上放置一個空白數據源並通過ID使用它。 – 2012-01-06 15:57:53