Transforming XML Documents to HTML using .NET Transformation

The XML Data - mcBooks.xml

I have an XML document that stores data about books. Some of the XML tags in the XML document are author name, book title, category, price, and summary. The XML document that stores data looks like the following - 

<?

xml version="1.0"?>
<
books>
<book>
<author>Mahesh Chand</author>
<title>A Programmer's Guide to ADO.NET in C#</title>
<mclink>http://www.c-sharpcorner.com/Store/1001Details.asp</mclink>
<category>ADO.NET/Database/C#/.NET</category>
<price currency="USD">44.99</price>
<summary>
Learn how to write Windows and Web based database
applications using ADO.NET and C#.
</summary>
<About>
Mahesh Chand, .NET consultant and author has been working with .NET since its beta released. He is also the founder of C# Corner (http://www.c-sharpcorner.com) and (http://www.mindcracker.com) Web sites.
</About>
</book>
<book>
<author>David Talbot and Mahesh Chand</author>
<mclink>http://www.c-sharpcorner.com/Store/Books/0732Details.asp</mclink>
<title>Applied ADO.NET: Building Data-Driven Solutions</title>
<category>ADO.NET/Database/VB.NET</category>
<price currency="USD">44.99</price>
<summary>
Learn how to write Windows and Web based database
applications using ADO.NET and VB.NET.
</summary>
<About>
Mahesh Chand, .NET consultant and author has been working with .NET since its beta released. He isalso the founder of C# Corner (http://www.c-sharpcorner.com)
and (http://www.mindcracker.com) Web sites.
</About>
</book>
<book>
<author>Mahesh Chand</author>
<mclink>http://www.c-sharpcorner.com/Store/Books/VisualCsharp.asp</mclink>
<title>The Complete Visual C# Programmer's Guide</title>
<category>Visual C#/.NET</category>
<price currency="USD">59.95</price>
<summary>
Learn how to write .NET applications using Visual C#
</summary>
<About>
Bulent Ozkir, Mike Gold, Mahesh Chand, Saurabh Nandu, Shivani Maheshwari.
</About>
</book>
</
books>

The XSLT File : mcStyles.xsl 

The purpose of XSLT file is to define the rules of formatting of XML data. Our XSLT file looks like the following -

<?

xml version="1.0"?>
<
xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/">
<html>
<body>
<table border="1" cellpadding="5">
<tr>
<th>Title</th>
<th>Author</th>
<th>Price</th>
<th>Category</th>
<th>Details</th>
<th>About the Author</th>
</tr>
<!-- Looping -->
<xsl:for-each select="books/book" >
<xsl:sort select="title" order="descending" />
<tr>
<xsl:if test="author='Mahesh Chand'">
<xsl:attribute name="bgcolor">red</xsl:attribute>
</xsl:if>
<!-- Exporting hyperlink tag -->
<td>
<a href="{mclink}"><xsl:value-of select="title"/></a>
</td>
<td><xsl:value-of select="author" /></td>
<td><xsl:value-of select="price" /></td>
<td><xsl:value-of select="category" /></td>
<td><xsl:value-of select="summary" /></td>
<td><xsl:value-of select="About" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</
xsl:template>
</xsl:stylesheet>

The Transformation Process 

Once we have the data and the format style sheet, we need to apply the style sheet to the XML document. The .NET framework library provides XslTransform class, which is defined in the System.Xml.Xsl namespace. Before we use this class, we need to add reference to the following namespace: 

using

System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;

Now we can call XslTransform.Transform method, which applies a style sheet to the XML data. The following source code shows we first loads the xsl file using the Load method and calls the Transform method. The Transform method takes two parameters - the source file and the output file name. As you can see, we are using mcBooks.xml as the source file and the output is mcFile.html. 

private

void TransformBtn_Click(object sender, System.EventArgs e)
{
XslTransform xslt =
new XslTransform();
xslt.Load(@"mcStyles.xsl");
xslt.Transform("mcBooks.xml", "mcFile.html");
}

Note: You can write this code on a button click event handler of a Windows or Web application. 

The Output: mcFile.html 

Now our formatted XML data looks like Figure 1 in HTML format. As you can see from this figure, we can easily format our XML data and design interactive pages. Now this approach is more useful, when you need to generate customize user interfaces based on the user selections. 

 

Updated Source Code

The above code is obselete. Here is the latest code:

/// <summary>
/// Transforms XML to HTML using XSLT
/// </summary>
/// <param name="xml">XML Document</param>
/// <param name="xslt">Stylesheet</param>
/// <param name="writer">XmlTextWriter - Output</param>
/// <example>
/// string xmlFile = @"C:\a.xml";
/// string xslFile = @"C:\b.xsl";
/// XmlTextWriter htmlWriter = new XmlTextWriter(@"C:\output.html", null);
/// TransformXML(xmlFile, xslFile, htmlWriter);
/// htmlWriter.Close();
/// </example>

public bool TransformXML(string Xml, string Xslt, XmlTextWriter Writer)
{
XslTransform transformator
= new XslTransform();
try
{
// Load stylesheet
transformator.Load(Xslt);
// Create DOM Tree
XPathDocument document = new XPathDocument(Xml);
// Apply transformation
transformator.Transform(document, null, Writer, null);
}
catch(Exception exp)
{
// To avoid the warning
string str = exp.Message;
return false;
}
// Every thing went well
return true;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值