In this article we will introduce with the XML control in ASP.NET using C#.
XML Example in ASP.NET using C#
The XML control in ASP.NET is used to read the XML file. Another file is .xsl file which is used to display the XML data with formatting. The DocumentSource property sets the path of XML file and TransformSource property sets the path of .Xsl file. In this example we make a XML file(Employee.xml) and a xsl file(Employee.xsl).
Employee.xml file
<?xml version="1.0" encoding="utf-8" ?> <employee> <person> <name>Manish</name> <post>Java Developer</post> <department>Development</department> <mobileno>8785685868</mobileno> <city>Delhi</city> <age>26</age> </person> <person> <name>Rahul</name> <post>Manager</post> <department>Account</department> <mobileno>453543546</mobileno> <city>Banglore</city> <age>30</age> </person> <person> <name>Ritesh</name> <post>PHP Developer</post> <department>Development</department> <mobileno>567457457</mobileno> <city>Mumbai</city> <age>28</age> </person> <person> <name>Amit</name> <post>Engineer</post> <department>Civil</department> <mobileno>435464666</mobileno> <city>Noida</city> <age>35</age> </person> </employee>
Employee.xsl file
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"> <xsl:template match="/"> <html> <body> <h2><xsl:value-of select="employee/name" /></h2> <table width="300" border="1"> <xsl:for-each select="employee/person"> <tr> <td> <b>Name: <xsl:value-of select="name" /></b><br /> Post: <xsl:value-of select="post" /><br /> Department: <xsl:value-of select="department" /><br /> Mobile No.: <xsl:value-of select="mobileno" /><br /> City: <xsl:value-of select="city" /><br /> Age: <xsl:value-of select="age" /> </td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
XML.aspx (Design page):
XML.aspx (source code):
<%@ Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master" AutoEventWireup="true" CodeFile="XML.aspx.cs" Inherits="XML" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> <div> <h2 style="color: Green"> XML in ASP.NET 4 , C#</h2> <asp:Xml ID="Xml1" runat="server" DocumentSource="~/XML/Employee.xml" TransformSource="~/Employee.xslt"></asp:Xml> </div> </asp:Content>
Output:
The following image shows the output of an XML file with formatting.
Download source code
[ 0 ] Comments