tags:

views:

106

answers:

4

I want to read an XML file located here

The data looks like this

<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]]&gt;&lt;/avatarIcon&gt; 
    <avatarMedium><![CDATA[http://media.steampowered.com/steamcommunity/public/images/avatars/9d/9d13f8f8061b7adaa9e9c2766e2bb2b3d82f911c_medium.jpg]]&gt;&lt;/avatarMedium&gt; 
    <avatarFull><![CDATA[http://media.steampowered.com/steamcommunity/public/images/avatars/9d/9d13f8f8061b7adaa9e9c2766e2bb2b3d82f911c_full.jpg]]&gt;&lt;/avatarFull&gt; 
    <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.

+1  A: 

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.

TheGeekYouNeed
+2  A: 
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);
        }
    }
}
Darin Dimitrov
I see it's gonna be one of those nights. ;-)
Sky Sanders
+1  A: 
        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;
Sky Sanders
+2  A: 
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.

hoang