Posts tagged: how to use asp.net xml file

Sep 17 2009

asp.net xml control

Asp.net Xml control is a server control used to display an xml document and the result of an XSL Transform

Properties of XML control

Document: it specifies an xml document using namespace System.Xml.XmlDocument

DocumentContent: it specifies an XML string

DocumentSource: It specifies path of an XML file

TransformSource: It specifies path of an XSL file

Asp.net XML control Example:

Step 1: first Create xml file

goto VS2005 or VS2008 website menu – choose Add

New Item — choose XML file – give name register.xml

The xml file looks like

<?xml version=”1.0″ encoding=”utf-8″ ?>

Then extend below code also

<registration>

<userdata>

<firstname>santhu</firstname>

<mobileno>98480</mobileno>

<country>India</country>

<emailId>santhweb</emailId>

<loginname>santhu</loginname>

<loginPwd>web123</loginPwd>

</userdata>

<userdata>

<firstname>raju</firstname>

<mobileno>98480</mobileno>

<country>India</country>

<emailId>rajuweb</emailId>

<loginname>raju</loginname>

<loginPwd>raju123</loginPwd>

</userdata>

<userdata>

<firstname>kiran</firstname>

<mobileno>99480</mobileno>

<country>India</country>

<emailId>kiranweb</emailId>

<loginname>kiran</loginname>

<loginPwd>kiran222</loginPwd>

</userdata>

<userdata>

<firstname>rakesh</firstname>

<mobileno>99480</mobileno>

<country>India</country>

<emailId>rakeshweb</emailId>

<loginname>rakesh </loginname>

<loginPwd>rak345</loginPwd>

</userdata>

<userdata>

<firstname>john</firstname>

<mobileno>9052</mobileno>

<country>India</country>

<emailId>johnweb</emailId>

<loginname>john</loginname>

<loginPwd>john234</loginPwd>

</userdata>

</registration>

Step 2: Create XSLT File

goto VS2005 or VS2008 website menu – choose Add

New Item — choose XSLT File – give name register.xslt

The XSLT File looks like

<?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:output method=”xml” indent=”yes”/>

<xsl:template match=”@* | node()”>

<xsl:copy>

<xsl:apply-templates select=”@* | node()”/>

</xsl:copy>

</xsl:template>

</xsl:stylesheet>

In above XSLT File remove below code or keep in comment

<xsl:template match=”@* | node()”>–>

<!–<xsl:copy>

<xsl:apply-templates select=”@* | node()”/>

</xsl:copy>–>

OR

Modify     <xsl:template match=”@* | node()”>

To

<xsl:template match=”/”>  this will effect to retrieve XSLT file

<xsl:for-each select=”registration/userdata“>

Registration is parent tag of xml file <registration></registration>

Userdate is subparent tag of xml file <userdata>  </userdata>

ChildDate

Retreiving xml child element to XSLT file use below syntax

<firstname>john</firstname>

<mobileno>9052</mobileno>

<country>India</country>

<emailId>johnweb</emailId>

<loginname>john</loginname>

<loginPwd>john234</loginPwd>

<xsl:value-of select=“firstname”/>

Complete Code for XSLT 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:output method=”xml” indent=”yes”/>

<!–<xsl:template match=”@* | node()”>–>

<!–<xsl:copy>

<xsl:apply-templates select=”@* | node()”/>

</xsl:copy>–>

<xsl:template match=”/”>

<html>

<body>

<h2>Registration Users</h2>

<table border=”1″>

<tr bgcolor=”#c6c6c6″>

<th align=”left”>FirstName</th>

<th align=”left”>MobileNo</th>

<th align=”left”>Country</th>

</tr>

<xsl:for-each select=”registration/userdata”>

<tr>

<td>

<xsl:value-of select=”firstname”/>

</td>

<td>

<xsl:value-of select=”mobileno”/>

</td>

<td>

<xsl:value-of select=”country”/>

</td>

</tr>

</xsl:for-each>

</table>

</body>

</html>

</xsl:template>

</xsl:stylesheet>

Step 3: Drag asp.net XML control in Default.aspx page

Goto properties of Asp.net XML Control — click DocumentSource choose path of the xml file (register.xml)

Similarly,Click property of TransformSource in asp.net XML control –choose register.xslt file

<asp:Xml ID=”Xml1″ runat=”server”

DocumentSource=”~/register.xml”

TransformSource=”~/register.xslt”>

</asp:Xml>

Step 4: Run the Application

asp.net xml control result

asp.net xml control result

Alibi3col theme by Themocracy