2
我正在處理一個需要我將xml文檔加載到xslt並將其轉換爲在html表中顯示的項目。問題在於,因爲我使用LINQ創建XML,所有元素標記都是XElement標記,所以「xsl:value-of select」屬性不會讀取XElement。我想知道是否有方法將XElement轉換爲XmlElement,或者只是讓XSLT讀取XElements而不是XMLElements?將XElement轉換爲XmlElement以進行XSLT轉換ASP.NET C#
這裏就是我通過LINQ數據加載到XML文件中的代碼:
List<Prod> Products = utils.getProducts();
XElement xml = new XElement("Products", Products.Select(x => new XElement("Product",
new XElement("ProductID", x.ProductID),
new XElement("ProductName", x.ProductName),
new XElement("SupplierID", x.SupplierID),
new XElement("CategoryID", x.CategoryID),
new XElement("QuantityPerUnit", x.QuantityPerUnit),
new XElement("UnitPrice", x.UnitPrice),
new XElement("UnitInStock", x.UnitInStock),
new XElement("UnitsOnOrder", x.UnitsOnOrder),
new XElement("ReorderLevel", x.ReorderLevel))));
xml.Save("C:/Users/Aaron/Documents/Visual Studio 2012/WebSites/INFO451Final/Part_B/Prod.xml");
這裏是代碼爲我的XSLT:
<xsl:template match="/">
<html>
<body>
<h2>Products</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>ProductID</th>
<th>ProductName</th>
<th>SupplierID</th>
<th>CategoryID</th>
<th>QuantityPerUnit</th>
<th>UnitPrice</th>
<th>UnitInStock</th>
<th>UnitsOnOrder</th>
<th>ReorderLevel</th>
</tr>
<xsl:for-each select="Product">
<tr>
<td>
<xsl:value-of select="ProductID"/>
</td>
<td>
<xsl:value-of select="ProductName"/>
</td>
<td>
<xsl:value-of select="SupplierID"/>
</td>
<td>
<xsl:value-of select="CategoryID"/>
</td>
<td>
<xsl:value-of select="QuantityPerUnit"/>
</td>
<td>
<xsl:value-of select="UnitPrice"/>
</td>
<td>
<xsl:value-of select="UnitInStock"/>
</td>
<td>
<xsl:value-of select="UnitsOnOrder"/>
</td>
<td>
<xsl:value-of select="ReorderLevel"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
所以,現在所有加載的都是標題,因爲「xsl:for-each」也不起作用。
任何幫助將不勝感激。
我剛剛完成了「將XElement轉換爲XmlElement」的網頁搜索,並收到了很多有前景的點擊。你有沒有嘗試過,如果是的話,發生了什麼? – 2013-05-07 20:37:43
我試過,但是,我一直沒有能夠實現解決方案到一個HTML表格,這是我需要能夠做到的。 – Fuzzerker 2013-05-07 20:50:43
那麼,是由於未能將XElement轉換爲XmlElement或其他東西?能夠分離任務很重要。 – 2013-05-07 21:45:11