-2

I have a question:

How to parse XML in this specific document: http://steamcommunity.com/groups/BotBrothers/memberslistxml/?xml=1&p=1

I want to get all the <STEAMID64>'s in a file document line by line

It's not duplicate of that other one, I need this one cuz it works diffrent.

MyName
  • 7
  • 6
  • 1
    possible duplicate of [How does one parse XML files?](http://stackoverflow.com/questions/55828/how-does-one-parse-xml-files) – Tim Aug 15 '15 at 16:38

1 Answers1

1

Try code below. I had to change 1st line from standalone="true" to standalone="yes"

using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string file = File.ReadAllText(FILENAME);
            file = file.Replace("standalone=\"true\"", "standalone=\"yes\"");
            StringReader reader = new StringReader(file);
            XmlSerializer xs = new XmlSerializer(typeof(MemberList));
            MemberList  members = (MemberList)xs.Deserialize(reader);
        }
    }
    [XmlRoot("memberList")]
    public class MemberList
    {
        [XmlElement("groupID64")]
        public string groupID64 { get; set;}
        [XmlElement("groupDetails")] 
        public GroupDetails groupDetails { get; set;} 
        [XmlElement("nextPageLink")]
        public Text nextPageLink { get; set; }
        [XmlElement("members")]
        public Members members { get; set; }

    }

    [XmlRoot("groupDetails")]
    public class GroupDetails
    {
        [XmlElement("groupName")]
        public Text groupName { get; set; }
        [XmlElement("groupURL")]
        public Text groupURL { get; set; }
        [XmlElement("headline")]
        public Text headline { get; set; }
        [XmlElement("summary")]
        public Text summary { get; set; }
        [XmlElement("avatarIcon")]
        public Text avatarIcon { get; set; }
        [XmlElement("avatarMedium")]
        public Text avatarMedium { get; set; }
        [XmlElement("avatarFull")]
        public Text avatarFull { get; set; }
        [XmlElement("memberCount")]
        public int memberCount { get; set; }
        [XmlElement("membersInChat")]
        public int membersInChat { get; set; }
        [XmlElement("membersInGame")]
        public int membersInGame { get; set; }
        [XmlElement("membersOnline")]
        public int membersOnline { get; set; }

    }
    public class Text
    {
        [XmlText]
        public string text { get; set;}
    }

    [XmlRoot("members")]
    public class Members
    {
        [XmlElement("steamID64")]
        public List<string> steamID64 { get; set;}
    }
}
​

Simple solution to get the steamID64 and fix the error in the XML

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string file = File.ReadAllText(FILENAME);
            file = file.Replace("standalone=\"true\"", "standalone=\"yes\"");
            XDocument doc = XDocument.Parse(file);

            List<string> steamID64 = doc.Descendants("steamID64").Select(x => (string)x).ToList();
        }
    }

}​
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • While this will work, if you have to edit the input XML it is less than an ideal solution. And generally speaking (personal preference here), simply posting a wall of code without explanation makes for a very poor answer - both for the OP (who appears to be new to XML parsing) and any future readers looking for answers. – Tim Aug 15 '15 at 16:40
  • Error at line "MemberList members = (MemberList)xs.Deserialize(reader);" – MyName Aug 15 '15 at 17:34
  • Also, I just need the STEAMID64's, Not the others – MyName Aug 15 '15 at 17:46
  • You can eliminate any object in the classes you don't need. I modified the code to fix the error in the XML to change "true" to "yes". – jdweng Aug 15 '15 at 18:22
  • Tim : How do you expect to read a file that contains error. Only real solution is to fix the error. If you are not the owner of the code that generated the error, then you have to do something as a work around. – jdweng Aug 15 '15 at 18:24
  • @jdweng - Look for other solutions that don't require you to change the file. Changing the file should be the last resort (IMO). LINQ to XML would probably do just fine, and the code would be simpler to write as well. `var steamIDs - xDoc.Descendants("steamID64");` would return an `IEnumerable` collection of all the "steamID64" elements, for example. (This assumes that `xDoc` is an `XDocument` representation of the XML, of course). – Tim Aug 15 '15 at 18:38