Reputation: 4999
How can I get the data from this xml site ( http://xboxapi.duncanmackenzie.net/gamertag.ashx?GamerTag=Festive+Turkey ) and specify the data and use it as a string?
Upvotes: 3
Views: 6028
Reputation: 126864
If you want to get the XML into a readable data structure directly, you can load it via the URI directly into an XDocument object.
string uri = "http://xboxapi.duncanmackenzie.net/gamertag.ashx?GamerTag=Festive+Turkey";
XDocument document = XDocument.Load(uri);
You can then easily extract the information via Linq-to-XML into either a concrete or anonymous type. (Check the System.Linq and System.Xml.Linq namespaces.)
Other methods of dealing with the XML include serialization, XmlDocuments and XPath, etc.
Here is a sample of something you could do with XML and Linq.
using System;
using System.Linq;
using System.Xml.Linq;
class XboxStats
{
static void Main()
{
string uri = "http://xboxapi.duncanmackenzie.net/gamertag.ashx?GamerTag=Festive+Turkey";
XDocument document = XDocument.Load(uri);
var xboxInfo = document.Element("XboxInfo");
var data = new
{
AccountStatus = (string)xboxInfo.Element("AccountStatus"),
PresenceInfo = new
{
Valid = (bool)xboxInfo.Element("PresenceInfo").Element("Valid"),
Info = (string)xboxInfo.Element("PresenceInfo").Element("Info"),
Info2 = (string)xboxInfo.Element("PresenceInfo").Element("Info2"),
LastSeen = (DateTime)xboxInfo.Element("PresenceInfo").Element("LastSeen"),
Online = (bool)xboxInfo.Element("PresenceInfo").Element("Online"),
StatusText = (string)xboxInfo.Element("PresenceInfo").Element("StatusText"),
Title = (string)xboxInfo.Element("PresenceInfo").Element("Title")
},
State = (string)xboxInfo.Element("State"),
Gamertag = (string)xboxInfo.Element("Gamertag"),
ProfileUrl = (string)xboxInfo.Element("ProfileUrl"),
TileUrl = (string)xboxInfo.Element("TileUrl"),
Country = (string)xboxInfo.Element("Country"),
Reputation = (decimal)xboxInfo.Element("Reputation"),
Bio = (string)xboxInfo.Element("Bio"),
Location = (string)xboxInfo.Element("Location"),
ReputationImageUrl = (string)xboxInfo.Element("ReputationImageUrl"),
GamerScore = (int)xboxInfo.Element("GamerScore"),
Zone = (string)xboxInfo.Element("Zone"),
RecentGames = new
{
XboxUserGameInfos = from gameInfo in xboxInfo.Element("RecentGames").Elements("XboxUserGameInfo")
select new
{
Game = new
{
Name = (string)gameInfo.Element("Game").Element("Name"),
TotalAchievements = (int)gameInfo.Element("Game").Element("TotalAchievements"),
TotalGamerScore = (int)gameInfo.Element("Game").Element("TotalGamerScore"),
Image32Url = (string)gameInfo.Element("Game").Element("Image32Url"),
Image64Url = (string)gameInfo.Element("Game").Element("Image64Url")
},
LastPlayed = (DateTime)gameInfo.Element("LastPlayed"),
Achievements = (int)gameInfo.Element("Achievements"),
GamerScore = (int)gameInfo.Element("GamerScore"),
DetailsUrl = (string)gameInfo.Element("DetailsUrl")
}
}
};
Console.WriteLine(data.AccountStatus);
foreach (var gameInfo in data.RecentGames.XboxUserGameInfos)
{
Console.WriteLine(gameInfo.Game.Name);
}
Console.Read();
}
}
Upvotes: 3
Reputation: 887449
You can use the WebClient
class:
Uri url = new Uri("http://xboxapi.duncanmackenzie.net/gamertag.ashx?GamerTag=" + Uri.EscapeDataString(str));
using (var wc = new WebClient()) {
return request.DownloadString(url);
}
Upvotes: 0
Reputation: 18013
Use a WebClient object
public static string GetWebPage(Uri uri) {
if ((uri == null)) {
throw new ArgumentNullException("uri");
}
using (var request = new WebClient()) {
//Download the data
var requestData = request.DownloadData(uri);
//Return the data by encoding it back to text!
return Encoding.ASCII.GetString(requestData);
}
}
Upvotes: 6