ColdFusion & .NET: Parsing an XML Feed (Xbox Live)

Today’s post in our series comparing ColdFusion and .NET covers the topic of parsing XML.  If you are a developer you do this quite often.  For this example I am parsing a feed that returns Xbox Live information for a user.  Microsoft does not officially provide this feed, but I came across a little web service from Duncan Mackenzie that provides this information.  He actually works for Microsoft and has been very generous in providing this service on his website. 

Both of the code samples below return the EXACT same output.  You can view the working ColdFusion code here and the working .NET code here.

I will cover the .NET code first today.  Just as a reminder this code is written in C# and uses the latest 3.5 version of the framework.  There are many different ways in .NET to parse XML, but I have chosen one of the newer features of the framework, LINQ to XML.  I will not cover LINQ in this post, as there is not enough time for that!  I will say that LINQ is cool and is worth a look when you have the time.  In this example, LINQ allows me to read in the XML document and then query it using the query syntax for LINQ.  Below are two sections, the first is the front end ASPX page and the second is the actual code behind page.  I am using a ListView control to handle the data display.  The back end code queries the document and then binds to the controls.

<code lang="csharp[lines]">

Account Status:
Last Seen:
Profile URL:
" />
Gamer Score:


And the code behind code:

<code lang="csharp[lines]"> using System;<br></br>
using System.Collections.Generic;<br></br> using System.Linq;<br></br> using System.Xml.Linq;<br></br> using System.Web;<br></br> using System.Web.UI;<br></br> using System.Web.UI.WebControls;<br></br> using System.Xml;

public partial class xbox : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
XDocument xboxFeed = XDocument.Load("");

var live = from item in xboxFeed.Descendants("XboxInfo")
select new
AccountStatus = item.Element("AccountStatus").Value,
LastSeen = item.Element("PresenceInfo").Element("Info").Value,
motto = item.Element("Motto").Value,
ProfileURL = item.Element("ProfileUrl").Value,
TileURL = item.Element("TileUrl").Value,
Country = item.Element("Country").Value,
Reputation = item.Element("Reputation").Value,
GamerScore = item.Element("GamerScore").Value,
Zone = item.Element("Zone").Value

var games = from item in xboxFeed.Descendants("XboxInfo").Elements("RecentGames").Elements("XboxUserGameInfo").Elements("Game")
select new
Name = item.Element("Name").Value

xboxInfo.DataSource = live;

Games.DataSource = games;

The ColdFusion code uses the XmlParse function to load up our document.

<code lang="cfm[lines]"><cfhttp method="GET" url=""></cfhttp>

xmlfile = xmlparse(cfhttp.filecontent);

accountStatus = xmlfile.XboxInfo.AccountStatus.XmlText;
writeOutput('Account Status: ' & accountStatus & '

lastSeen = xmlfile.XboxInfo.PresenceInfo.Info.XmlText;
writeOutput('Last Seen: ' & lastSeen & '

motto = xmlfile.XboxInfo.motto.XmlText;
writeOutput('Motto: ' & motto & '

profileURL = xmlfile.XboxInfo.ProfileUrl.XmlText;
writeOutput('Profile URL: ' & profileURL & '

image = xmlfile.XboxInfo.TileUrl.XmlText;

country = xmlfile.XboxInfo.Country.XmlText;
writeOutput('Country: ' & country & '

reputation = xmlfile.XboxInfo.Reputation.XmlText;
writeOutput('Reputation: ' & reputation & '

gamerScore = xmlfile.XboxInfo.GamerScore.XmlText;
writeOutput('Gamer Score: ' & gamerScore & '

zone = xmlfile.XboxInfo.Zone.XmlText;
writeOutput('Zone: ' & zone & '


gamesSize = arraylen(xmlfile.XboxInfo.RecentGames.xmlchildren);
for(x=1; x LTE gamesSize; x=x+1) {
writeOutput(xmlfile.XboxInfo.RecentGames.XboxuserGameInfo[x].Game.Name.XmlText & '

You can probably figure out above that the games are contained in their own node, so in the .NET code I query to get them and the ColdFusion code uses an ArrayLen function to find the number of nodes and then loops over them.

In this example, the ColdFusion code is a little smaller and easier to follow.  I will point out that the .NET example is just the tip of the iceberg as far as LINQ to XML goes.  The querying features in it are very powerful for large data files.


comments powered by Disqus