开发者

Using ASP.NET How Do I Read External XML from Website?

I want to read an XML file located here

T开发者_JAVA百科he data looks like this

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

And I just want to be able to access those values. My limited knowledge of XmlTextReaders has lead me no where. Thank you.


        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;


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


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

This way you have a Profile Object and can access its values using properties.


To get the XML, look at the HttpWebRequest. Use that to get a stream of the XML at that URL, then to grab what you need, as jarrett suggested, use Linq to grab the data you need.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜