C# - 从在线xml文件中获取数据

时间:2010-05-31 21:31:58

标签: c#

如何从此xml站点(http://xboxapi.duncanmackenzie.net/gamertag.ashx?GamerTag=Festive+Turkey)获取数据并指定数据并将其用作字符串?

3 个答案:

答案 0 :(得分:6)

使用WebClient对象

    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);
        }
    }

答案 1 :(得分:3)

如果要将XML直接转换为可读数据结构,可以通过URI将其直接加载到XDocument对象中。

string uri = "http://xboxapi.duncanmackenzie.net/gamertag.ashx?GamerTag=Festive+Turkey";
XDocument document = XDocument.Load(uri);

然后,您可以通过Linq-to-XML轻松地将信息提取为具体或匿名类型。 (检查System.Linq和System.Xml.Linq名称空间。)

处理XML的其他方法包括序列化,XmlDocuments和XPath等。

以下是您可以使用XML和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();
    }
}

答案 2 :(得分:0)

您可以使用WebClient类:

Uri url = new Uri("http://xboxapi.duncanmackenzie.net/gamertag.ashx?GamerTag=" + Uri.EscapeDataString(str));
using (var wc = new WebClient()) {
    return request.DownloadString(url);
}