Parsing the WoW Armory

February 7, 2008

One of my side projects is working on a WoW Guild Roster. I’ve been doing a lot of work over the past couple months with Visual Studio 2008, although I’ve been stuck with ASP.NET v2 since DNN won’t be supporting v3.5 until DNN v5. One of the features that has me really excited within .NET v3.5 is LINQ.

So, while I was playing with LINQ, I decided ‘what would the code look like if I were to query WoW character data from the WoW Armory? So…I sat down and wrote a quick query of character information.

1 XDocument _charSheet; 2 3 System.Net.WebClient _wc = new System.Net.WebClient(); 4 _wc.QueryString.Add("r", this.Realm); 5 _wc.QueryString.Add("n", this.CharName); 6 _wc.Headers.Add("user-agent", "MSIE 7.0"); 7 System.Xml.XmlTextReader _reader = new System.Xml.XmlTextReader(_wc.OpenRead(ArmoryCharSheet)); 8 9 _charSheet = XDocument.Load(_reader); 10 IEnumerable<XElement> _charInfoEl = _charSheet.Root.Descendants("characterInfo"); 11 if (_charInfoEl.Count() < 1) { 12 MessageBox.Show("No descendants at <characterInfo>"); 13 } else if (_charInfoEl.Count() > 1) { 14 MessageBox.Show("Multiple descendants at <characterInfo>"); 15 } 16 17 var _charInfo = from item in _charInfoEl.Descendants("character") 18 select new { 19 Battlegroup = item.Attribute("battleGroup").Value, 20 CharURL = item.Attribute("charUrl").Value, 21 Class = item.Attribute("class").Value, 22 ClassID = item.Attribute("classId").Value, 23 Faction = item.Attribute("faction").Value, 24 FactionID = item.Attribute("factionId").Value, 25 Gender = item.Attribute("gender").Value, 26 GenderID = item.Attribute("genderId").Value, 27 GuildName = item.Attribute("guildName").Value, 28 GuildURL = item.Attribute("guildUrl").Value, 29 LastModifiedString = item.Attribute("lastModified").Value, 30 Level = item.Attribute("level").Value, 31 Name = item.Attribute("name").Value, 32 Prefix = item.Attribute("prefix").Value, 33 Race = item.Attribute("race").Value, 34 RaceID = item.Attribute("raceId").Value, 35 Realm = item.Attribute("realm").Value, 36 Suffix = item.Attribute("suffix").Value 37 }; 38 IEnumerable<XElement> _profsEl = _charInfoEl.Descendants("professions"); 39 var _profs = from item in _profsEl.Descendants("skill") 40 select new { 41 Key = item.Attribute("key").Value, 42 Name = item.Attribute("max").Value, 43 Max = item.Attribute("name").Value, 44 Value = item.Attribute("value").Value 45 }; 46 IEnumerable<XElement> _baseStatsEl = _charInfoEl.Descendants("baseStats"); 47 var _baseStats = from item in _baseStatsEl.Descendants() 48 select new { 49 Stat = item.Name.ToString(), 50 Base = item.Attribute("base").Value, 51 Effective = item.Attribute("effective").Value, 52 Element = item 53 }; 54

I was quite impressed by the brevity of the code needed to get this. I still want to go back and clean up the web request to use some of WCF’s new capabilities, but I think the LINQ aspects of the code really speaks for itself. The code that it currently takes in .NET v2 to crawl/navigate the XML files was much much larger.

Cliff Simpkins

Posts