Transforming XML Documents to HTML using .NET Transformation

来源:互联网 发布:手机淘宝怎么买运费险 编辑:程序博客网 时间:2024/06/16 19:06

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 - 

<?

xmlversion="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>
<pricecurrency="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>
<pricecurrency="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>
<pricecurrency="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 -

<?

xmlversion="1.0"?>
<
xsl:stylesheetxmlns:xsl="http://www.w3.org/1999/XSL/Transform"version="1.0"><xsl:templatematch="/">
<html>
<body>
<tableborder="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-eachselect="books/book">
<xsl:sortselect="title"order="descending"/>
<tr>
<xsl:iftest="author='Mahesh Chand'">
<xsl:attributename="bgcolor">red</xsl:attribute>
</xsl:if>
<!-- Exporting hyperlink tag -->
<td>
<ahref="{mclink}"><xsl:value-ofselect="title"/></a>
</td>
<td><xsl:value-ofselect="author"/></td>
<td><xsl:value-ofselect="price"/></td>
<td><xsl:value-ofselect="category"/></td>
<td><xsl:value-ofselect="summary"/></td>
<td><xsl:value-ofselect="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>

publicbool 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;
returnfalse;
}
// Every thing went well
returntrue;
}

原创粉丝点击