共用方式為


Parsing the WoW Armory

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.

XDocument _charSheet;

System.Net.WebClient _wc = new System.Net.WebClient();
_wc.QueryString.Add("r", this.Realm);
_wc.QueryString.Add("n", this.CharName);
_wc.Headers.Add("user-agent", "MSIE 7.0");
System.Xml.XmlTextReader _reader = new System.Xml.XmlTextReader(_wc.OpenRead(ArmoryCharSheet));

_charSheet = XDocument.Load(_reader);
IEnumerable<XElement> _charInfoEl = _charSheet.Root.Descendants("characterInfo");
if (_charInfoEl.Count() < 1) {
MessageBox.Show("No descendants at <characterInfo>");
} else if (_charInfoEl.Count() > 1) {
MessageBox.Show("Multiple descendants at <characterInfo>");
}

var _charInfo = from item in _charInfoEl.Descendants("character")
select new {
Battlegroup = item.Attribute("battleGroup").Value,
CharURL = item.Attribute("charUrl").Value,
Class = item.Attribute("class").Value,
ClassID = item.Attribute("classId").Value,
Faction = item.Attribute("faction").Value,
FactionID = item.Attribute("factionId").Value,
Gender = item.Attribute("gender").Value,
GenderID = item.Attribute("genderId").Value,
GuildName = item.Attribute("guildName").Value,
GuildURL = item.Attribute("guildUrl").Value,
LastModifiedString = item.Attribute("lastModified").Value,
Level = item.Attribute("level").Value,
Name = item.Attribute("name").Value,
Prefix = item.Attribute("prefix").Value,
Race = item.Attribute("race").Value,
RaceID = item.Attribute("raceId").Value,
Realm = item.Attribute("realm").Value,
Suffix = item.Attribute("suffix").Value
};
IEnumerable<XElement> _profsEl = _charInfoEl.Descendants("professions");
var _profs = from item in _profsEl.Descendants("skill")
select new {
Key = item.Attribute("key").Value,
Name = item.Attribute("max").Value,
Max = item.Attribute("name").Value,
Value = item.Attribute("value").Value
};
IEnumerable<XElement> _baseStatsEl = _charInfoEl.Descendants("baseStats");
var _baseStats = from item in _baseStatsEl.Descendants()
select new {
Stat = item.Name.ToString(),
Base = item.Attribute("base").Value,
Effective = item.Attribute("effective").Value,
Element = item
};

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.

Comments

  • Anonymous
    May 27, 2010
    MM i wish u could explain this better (what each part does)