ASP.NET Reading External XML from a Website

I want to read the XML file located here

The data is as follows:

<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>

And I just want to have access to these values. My limited knowledge of XmlTextReaders did not lead me there. Thank.

+3
source share
4 answers
        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
source
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);
        }
    }
}
+2
source
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.

+2
source

To get XML, take a look at HttpWebRequest. Use this to get the XML stream at this URL, and then to understand what you need, as jarrett suggested, use Linq to capture the data you need.

+1
source

Source: https://habr.com/ru/post/1745930/


All Articles