ASP.NET从网站读取外部XML

时间:2010-05-18 07:32:52

标签: asp.net xml

我想阅读位于here

的XML文件

数据看起来像这样

<profile> 
    <steamID64>76561197967256555</steamID64> 
    <steamID><![CDATA[snivfbo]]></steamID> 
    <onlineState>offline</onlineState> 
    <stateMessage><![CDATA[]]></stateMessage> 
    <privacyState>private</privacyState> 
    <visibilityState>1</visibilityState> 
    <avatarIcon><![CDATA[http://media.steampowered.com/steamcommunity/public/images/avatars/9d/9d13f8f8061b7adaa9e9c2766e2bb2b3d82f911c.jpg]]></avatarIcon> 
    <avatarMedium><![CDATA[http://media.steampowered.com/steamcommunity/public/images/avatars/9d/9d13f8f8061b7adaa9e9c2766e2bb2b3d82f911c_medium.jpg]]></avatarMedium> 
    <avatarFull><![CDATA[http://media.steampowered.com/steamcommunity/public/images/avatars/9d/9d13f8f8061b7adaa9e9c2766e2bb2b3d82f911c_full.jpg]]></avatarFull> 
    <vacBanned>0</vacBanned> 
    <isLimitedAccount>0</isLimitedAccount> 
</profile>

我只是希望能够访问这些值。我对XmlTextReaders的了解有限,这让我无处可去。谢谢。

4 个答案:

答案 0 :(得分:2)

class Program
{
    static void Main(string[] args)
    {
        var doc = XDocument.Load("http://steamcommunity.com/profiles/76561197967256555/?xml=1");
        foreach (XElement node in doc.Root.Nodes())
        {
            Console.WriteLine("name:{0} | value:{1}", node.Name, node.Value);
        }
    }
}

答案 1 :(得分:2)

        XDocument doc = XDocument.Load("http://steamcommunity.com/profiles/76561197967256555/?xml=1");
        string steamID64 = doc.Root.Descendants("steamID64").First().Value;
        string steamID = doc.Root.Descendants("steamID").First().Value;
        string onlineState = doc.Root.Descendants("onlineState").First().Value;
        string stateMessage = doc.Root.Descendants("stateMessage").First().Value;
        string privacyState = doc.Root.Descendants("privacyState").First().Value;
        string visibilityState = doc.Root.Descendants("visibilityState").First().Value;
        string avatarIcon = doc.Root.Descendants("avatarIcon").First().Value;
        string avatarMedium = doc.Root.Descendants("avatarMedium").First().Value;
        string avatarFull = doc.Root.Descendants("avatarFull").First().Value;
        string vacBanned = doc.Root.Descendants("vacBanned").First().Value;
        string isLimitedAccount = doc.Root.Descendants("isLimitedAccount").First().Value;

答案 2 :(得分:2)

using System;
using System.Text;
using System.Xml.Serialization;

[Serializable]
[XmlRoot("profile")]
public class Profile
{
    [XmlElement("steamID64")]
    public string SteamId64 { get; set; }
    [XmlElement("steamID")]
    public string SteamId { get; set; }
    [XmlElement("onlineState")]
    public string OnlineState { get; set; }
    [XmlElement("stateMessage")]
    public string StateMessage { get; set; }
    [XmlElement("privacyState")]
    public string PrivacyState { get; set; }
    [XmlElement("visibilityState")]
    public string VisibilityState { get; set; }
    [XmlElement("avatarIcon")]
    public string AvatarIcon { get; set; }
    [XmlElement("avatarMedium")]
    public string AvatarMedium { get; set; }
    [XmlElement("avatarFull")]
    public string AvatarFull { get; set; }
    [XmlElement("vacBanned")]
    public string VacBanned { get; set; }
    [XmlElement("isLimitedAccount")]
    public string IsLimitedAccount { get; set; }

    public override string ToString()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("steamID64 : ").Append(this.SteamId64).Append("\n");
        sb.Append("steamID : ").Append(this.SteamId).Append("\n");
        sb.Append("onlineState : ").Append(this.OnlineState).Append("\n");
        sb.Append("stateMessage : ").Append(this.StateMessage).Append("\n");
        sb.Append("privacyState : ").Append(this.PrivacyState).Append("\n");
        sb.Append("visibilityState : ").Append(this.VisibilityState).Append("\n");
        sb.Append("avatarIcon : ").Append(this.AvatarIcon).Append("\n");
        sb.Append("avatarMedium : ").Append(this.AvatarMedium).Append("\n");
        sb.Append("avatarFull : ").Append(this.AvatarFull).Append("\n");
        sb.Append("vacBanned : ").Append(this.VacBanned).Append("\n");
        sb.Append("isLimitedAccount : ").Append(this.IsLimitedAccount).Append("\n");
        return sb.ToString();
    }
}

class Program
{
    static void Main(string[] args)
    {
        XmlReader reader = new XmlTextReader("http://steamcommunity.com/profiles/76561197967256555/?xml=1");

        XmlSerializer xs = new XmlSerializer(typeof(Profile), string.Empty);
        Profile p = xs.Deserialize(reader) as Profile;

        Console.WriteLine(p.ToString());
    }
}

通过这种方式,您可以使用配置文件对象,并可以使用属性访问其值。

答案 3 :(得分:1)

要获取XML,请查看HttpWebRequest。使用它来获取该URL的XML流,然后像jarrett建议的那样抓住你需要的东西,使用Linq来获取你需要的数据。