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