Reputation: 3803
Hi I am looking to get Listview Label text to display XML data obtained from SQL datasource. So far I have achieved to do this by string manipulation in codebehind/javascript. I am a noob and would like to know if there is any other way of achieving this?
My Xml file looks like this
<?xml ver="1-0" encoding="utf-8"?>
<artists>
<artist>
<name>fatfinger</name>
<id>1</id>
<artist>
<artist>.....</artist>
.
.
.
</artists>
I am manipulating string from code (ONDATABIND Event) behind so that the end result is
<asp:label id="label1" runat="server" text="String manipulation output here"
String manipulation output
<div class="artists"> <a class="artist" href="http://myweb.com/id">artist</a></div>
Is this approach correct? Or are there any other methods apart from javascript to achieve the same end results?
Upvotes: 1
Views: 1165
Reputation: 358
I would personally never try to do string manipulation/parsing on XML from scratch. There are a lot of built-in utilities in many languages and frameworks which handle that for you.
Below are three methods to do display XML data similar to what you want. You will still have to modify this to do exactly what you want, but these should give you some pretty good ideas. Method 3 is probably the closest, however it is best to know about as many options as possible so you use the best method for the scenario.
Method 1:
When I have to display xml data in a webpage, I prefer to use XSLT. However, doing so assumes your users are using an XSLT compatible browser.
artist.xml - This file includes a reference in the xml file itself.
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="artist.xsl"?>
<artists>
<artist>
<id>1</id>
<name>Chuck Berry</name>
</artist>
<artist>
<id>2</id>
<name>Blackfoot</name>
</artist>
<artist>
<id>3</id>
<name>Trixter</name>
</artist>
<artist>
<id>4</id>
<name>D'Molls</name>
</artist>
<artist>
<id>5</id>
<name>The Runaways</name>
</artist>
</artists>
artist.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<ul>
<xsl:for-each select="artists/artist">
<li><xsl:value-of select="name"/></li>
</xsl:for-each>
</ul>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Method 2:
This accomplishes the same thing as above, except it relies on .Net for the transformation. It also appears to display more reliably across browsers. A more robust example can be found here: http://support.microsoft.com/kb/315906.
For this, I created a simple c# WebForms project and put the xml and xsl files in the App_Data folder. I also removed the stylesheet reference from the xml file.
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SandboxWeb._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Test!
</h2>
<asp:Xml id="Xml1" runat="server" DocumentSource="~/App_Data/artist.xml" TransformSource="~/App_Data/artist.xsl"></asp:Xml>
</asp:Content>
Method 3:
Here is probably what you probably want. This also relies on artist.xml being in the App_Data folder.
Default.aspx
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="SandboxWeb._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Test!
</h2>
<asp:Repeater ID="repeater1" runat="server">
<ItemTemplate>
<p>
<asp:Label Id="lblName" runat="server" Text="<%# Container.DataItem %>" />
</p>
</ItemTemplate>
</asp:Repeater>
</asp:Content>
Default.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Xml.Linq;
namespace SandboxWeb {
public partial class _Default : System.Web.UI.Page {
private XDocument xmlDoc;
public List<String> ArtistNames { get; set; }
protected void Page_Load(object sender, EventArgs e) {
ArtistNames = new List<String>();
xmlDoc = XDocument.Load(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data/artist.xml"));
ArtistNames = (from a in xmlDoc.Root.Descendants("artist") select a.Element("name").Value).ToList<String>();
repeater1.DataSource = ArtistNames;
repeater1.DataBind();
}
}
}
Upvotes: 1