Asp.Net: Visualizzare dati XML in una web page
Operazione banale questa ed ormai consueta in moltissime applicazioni web..... scrivo qui una piccola demo per tutti!!!
NB: l'esempio si riferisce alla tabella "Authors" nel DB pubs di MSSql Server 2000, quindi il foglio di stile associato è creato ad hoc per questo esempio, è introdotto anche un XmlControl all'interno della pagina con il nome di XmlDisplay:
version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
match="/">
<style>
.header{font-weight:bold;color:white;background-color:black;}
.value{font-family:arial;font-size:.7em;background-color:silver}
</style>
<table border="1" cellspacing="0" cellpadding="1"
bordercolor="black">
<tr class="header">
<th>Author ID</th>
<th>Last Name</th>
<th>First Name</th>
<th>Phone</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>Zip</th>
<th>Contract</th>
</tr>
select='Schema1/authors'>
<tr>
<td nowrap="true" class="value">
<b>
select='@au_id' />
</b>
</td>
<td nowrap="true" class="value">
select='@au_lname' />
</td>
<td nowrap="true" class="value">
select='@au_fname' />
</td>
<td nowrap="true" class="value">
select='@phone' />
</td>
<td nowrap="true" class="value">
select='@address' />
</td>
<td nowrap="true" class="value">
select='@city' />
</td>
<td nowrap="true" class="value">
select='@state' />
</td>
<td nowrap="true" class="value">
select='@zip' />
</td>
<td nowrap="true" class="value">
select='@contract' />
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
Dim strCon As String
strCon = "data source=(local)\VSdotNET; " & _
"database=pubs;integrated security=true"
Dim sqlConn As New SqlConnection(ConnStr)
sqlConn.Open()
Try
Dim strSql As String
strSql = "SELECT * FROM authors FOR XML AUTO, XMLDATA"
Dim cmd As New SqlCommand(strSql, sqlConn)
Dim ds As New DataSet()
'riempio il dataset e passo il controllo XML con il metodo GetXML()
ds.ReadXml(cmd.ExecuteXmlReader(), _
XmlReadMode.Fragment)
XmlDisplay.DocumentContent = ds.GetXml()
Finally
sqlConn.Close()
End Try
powered by IMHO 1.2