0

This is my XML file. It fetched from a sql datatable using sql datasource from a Nvarchar record as string.

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Artist.xsl"?>
<artists>
<artist>
<name>KATY PERRY</name>
<id>1</id></artist>
<artist>
<name>SNOOP DOGG</name>
<id>2</id>
</artist>
</artists> 

The XSL style sheet is in the same folder as aspx page and looks like this

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match ="/">
<html>
  <body>
    <h2>Artist</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="/artists/artist">
        <tr>
          <td>
            <xsl:value-of select="name"/>
          </td>
          <td>
            <xsl:value-of select="id"/>
          </td>
        </tr>
      </xsl:for-each>
    </table>
  </body>
</html>
</xsl:template>
</xsl:stylesheet>

My aspx control is a literal inside a listview with the following markup

 <ItemTemplate>
        <td runat="server" style="">
            <asp:Literal ID="CoverartLabel" runat="server" Text='<%# Eval("Coverart")  %>'></asp:Literal>
            <a href="View.aspx/Album/<%# Eval("Id") %>"><%# Eval("Name") %></a>
            <br />
            <div>
            <asp:Literal ID="ArtistsLabel" runat="server" Text='<%# Eval("Artists")%>' Mode="Encode"></asp:Literal>
            <br />
                </div>
            <asp:Literal ID="SongsLabel" runat="server" Text='<%# Eval("Songs") %>'></asp:Literal>
            <asp:Label ID="LikesLabel" runat="server" Text='<%# Eval("Likes") %>' />
            <br />Comments:
            <asp:Label ID="CommentsLabel" runat="server" Text='<%# Eval("Comments") %>' />
            <br /></td>
    </ItemTemplate>

When I run the page I get the output as

enter image description here

Can some one help me figure out why the xml is not displayed as html. I ve tried changing the literal Artistlabel mode with no luck. Even played with the XSL file and its location?

Flood Gravemind
  • 3,773
  • 12
  • 47
  • 79

3 Answers3

1

You are not really applying the XSLT when you simply tell ASP.NET to display the XML in the literal's text. You have to apply the XSLT separately. From this question you can learn how to transform an XML:

XPathDocument myXPathDoc = new XPathDocument(myXmlFile) ;
XslCompiledTransform myXslTrans = new XslCompiledTransform();
myXslTrans.Load(myStyleSheet);
XmlTextWriter myWriter = new XmlTextWriter("result.html",null);
myXslTrans.Transform(myXPathDoc,null,myWriter);

Note the example is writing to a file but you would write to a string. You would then have to do that inside the ListView.ItemDataBound event for each bound row and generate the HTML you want.

Community
  • 1
  • 1
Tombala
  • 1,660
  • 9
  • 11
0

You are reading the xml as nvarchar type from database. This will consider it as a string rather an XMl. Convert string to xml and it should work.

Lets assume you have your xml as string fetched from databse in variable "xmlString". convert it to xml document.

      XmlDocument xdoc= new XmlDocument();
      xdoc.loadXml(xmlString);
0

Got it I used this on Itemdatabound in codebehind

 If e.Item.ItemType = ListViewItemType.DataItem Then
        Dim m As String = File.ReadAllText(Server.MapPath("Artist.xsl"))
        Dim kim As Literal = e.Item.FindControl("Artistslabel")
        Dim output As String
        Using srt As New System.IO.StringReader(m), sri As New IO.StringReader(kim.Text)
            Using xrt As XmlReader = XmlReader.Create(srt), xri As XmlReader = XmlReader.Create(sri)
                Dim xslt As New XslCompiledTransform()
                xslt.Load(xrt)
                Using sw As New System.IO.StringWriter(), xwo As XmlWriter = XmlWriter.Create(sw, xslt.OutputSettings)
                    xslt.Transform(xri, xwo)
                    output = sw.ToString
                End Using
            End Using
        End Using
        kim.Text = output
    End If
Flood Gravemind
  • 3,773
  • 12
  • 47
  • 79