Loading data from a XML file in asp.net


How to display data from an xml file using xslt and asp.net

Following lines of code will give you an insight of reading data from an xml file and applying your formats and styles using XSLT



Here's a xml file showing categories of products




<?xml version="1.0" encoding="utf-8" ?>

<categories>
<category pid='1' name='Men'>
<subcat prefid="11">Short sleeves</subcat>

<subcat prefid="12">Long Sleeves</subcat>
<subcat prefid="13">Tanks</subcat>
<subcat prefid="14">Sleeveless</subcat>
</category>
<category pid="2" name="Ladies" >
<subcat prefid= "21"> Short sleeves </subcat>

<subcat prefid="22"> Long Sleeves </subcat>
<subcat prefid="23"> Tanks </subcat>
</category>
<category pid="3" name="Athletic" >
<subcat prefid= "31"> Short sleeves </subcat>

<subcat prefid="32"> Long Sleeves </subcat>
<subcat prefid="33"> Tanks </subcat>
</category>

</categories>


Here is another file showing the how thjis file needs to be presented



<?xml version="1.0"?>
<!-- This is a non-standard style sheet designed just for
Internet Explorer. It will not work with any standards
compliant XSLT processor. -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html"/>

<xsl:template match="category">
<table width="50%" columns="2">
<th><td>
<xsl:variable name="category" select="@pid" />
<xsl:value-of select="@name"/>
</td><tr>
<td>
<img>
<xsl:attribute name="SRC">
<xsl:value-of select="@img" />
</xsl:attribute>
<xsl:attribute name="ALT">
<xsl:value-of select="@alt" />
</xsl:attribute>
</img>
</td>
</tr>

</th>
<tr><td>
<xsl:apply-templates/>
</td></tr></table>


</xsl:template>


<xsl:template match="/categories/category/subcat">


<td>




This is how the code will be calling the data and presentation together in an ASP.net file




<asp:Xml id="Xml1" runat="server" TransformSource="style.xslt" DocumentSource="categories.xml"></asp:Xml>


Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: