-4

I need to read the following XML docuemt and save all the fields in variables to use later on. Tried Linq but do't really understand how o use it. I need help, what code should I write to read the XML and save the fields in variables?

<?xml version="1.0" encoding="utf-8"?>

<Map name="Mapa Prueba" width="30" height="30" hp="500" orobyte="50" Tx="1" Ty="5" Sx="30" Sy="25"  >

    <StructuresList>
        <Structure name="Block de Notas" attack="0" i="1" j="1" range="0" price="8" />
        <Structure name="Excel" attack="2" i="2" j="2" range="1" price="20" />
        <Structure name="Word" attack="3" i="2" j="2" range="1" price="40" />
        <Structure name="PowerPoint" attack="3" i="3" j="2" range="1" price="50" />
        <Structure name="FireWall" attack="1" i="1" j="1" range="1" price="60" />
        <Structure name="Avast" attack="4" i="3" j="3" range="2" price="80" />
        <Structure name="Nod32" attack="4" i="3" j="2" range="2" price="110" />
        <Structure name="Panda" attack="6" i="3" j="2" range="1" price="130" />
        <Structure name="Norton" attack="5" i="3" j="3" range="2" price="150" />
        <Structure name="Eclipse" attack="4" i="3" j="4" range="3" price="180" />
        <Structure name="Visual Studio" attack="10" i="5" j="6" range="1" price="200" />
    </StructuresList>

    <EnemysList>
        <Enemy id="00" color="rojo" hp="20" orobyte="1" speed="1" />
        <Enemy id="01" color="azul" hp="10" orobyte="1" speed="2" />
        <Enemy id="02" color="amarillo" hp="35" orobyte="3" speed="1" />
        <Enemy id="03" color="celeste" hp="30" orobyte="2" speed="2" />
        <Enemy id="04" color="azul" hp="10" orobyte="1" speed="4" />
        <Enemy id="05" color="rojo" hp="60" orobyte="3" speed="1" />
        <Enemy id="06" color="celeste" hp="100" orobyte="10" speed="1" />
        <Enemy id="07" color="amarillo" hp="150" orobyte="25" speed="3" />
        <Enemy id="08" color="verde" hp="200" orobyte="40" speed="5" />
    </EnemysList>

    <Waves>
        <Wave>
            <Group id="00" count="10" />
            <Group id="01" count="5" />
        </Wave>
        <Wave>
            <Group id="00" count="15" />
            <Group id="01" count="5" />
        </Wave>
        <Wave>
            <Group id="01" count="25" />
        </Wave>
        <Wave>
            <Group id="00" count="24" />
            <Group id="01" count="5" />
        </Wave>
        <Wave>
            <Group id="02" count="20" />
        </Wave>
        <Wave>
            <Group id="02" count="10" />
            <Group id="03" count="20" />
        </Wave>
        <Wave>
            <Group id="03" count="20" />
            <Group id="07" count="1" />
        </Wave>
        <Wave>
            <Group id="03" count="10" />
            <Group id="04" count="30" />
        </Wave>
        <Wave>
            <Group id="05" count="30" />
            <Group id="06" count="10" />
            <Group id="07" count="1" />
        </Wave>
        <Wave>
            <Group id="08" count="3" />
        </Wave>
    </Waves>

</Map>
Tieson T.
  • 20,774
  • 6
  • 77
  • 92
  • http://www.daniweb.com/software-development/vbnet/threads/321453/read-xml-then-put-into-variables google your best friend :) – Bit Oct 17 '13 at 21:04
  • 1
    Please don't include tags in question titles. – user1306322 Oct 17 '13 at 21:06
  • 2
    "I need to read ..." - permission granted, go ahead and do that, "what code should I write" - you should write correct, readable code. Now if you have more specific questions (i.e. "why my following {sample code} LINQ code does return XXXX instead of YYYY") you question will be much better fir for SO. – Alexei Levenkov Oct 17 '13 at 21:09

3 Answers3

1

Take your pick of any of these already answered versions of your same question.

LINQ to read XML

Reading an XML using libxml2

Reading Xml with XmlReader in C#

Community
  • 1
  • 1
Garrett
  • 545
  • 1
  • 7
  • 21
0

If you want to use linq this is a basic example. For instance with this piece of code you will be retrieving all the Structure in StructureList and checking the attributes. It's quite easy, you will get an idea.

var xDoc = XDocument.Load(xmlFile); //Or XDocument.Parse(xmlString)

var structures = xDoc.Descendants("StructuresList").Descendants("Structure")
                     .ToList();

foreach (var structure in structures)
{
    var name = structure.Attribute("name").Value;
    var attack = structure.Attribute("attack").Value;
    //Continue checking attributes
}
Esteban Elverdin
  • 3,552
  • 1
  • 17
  • 21
0
static void Main(string[] args)
        {
            string path =@"file.xml";

            Map mymap = new Map();
            var xml = XDocument.Load(path);
            var map = xml.Descendants("Map").Select(x => new Map()
                {
                    Structures = x.Element("StructuresList").Elements("Structure").Select(r => new StructuresList()
                    {
                        Name = r.Attribute("name").Value,
                        Attack =Convert.ToInt32(r.Attribute("attack").Value),
                        I = Convert.ToInt32(r.Attribute("i").Value),
                        J = Convert.ToInt32(r.Attribute("j").Value),
                        Range = Convert.ToInt32(r.Attribute("range").Value),
                        Price = Convert.ToInt32(r.Attribute("price").Value)

                    }).ToList(),

                    Enemys = x.Element("EnemysList").Elements("Enemy").Select(r => new EnemysList()
                    {
                        ID = Convert.ToInt32(r.Attribute("id").Value),
                        Color = r.Attribute("color").Value.ToString(),
                        HP = Convert.ToInt32(r.Attribute("hp").Value),
                        Orobyte = Convert.ToInt32(r.Attribute("orobyte").Value),
                        Speed = Convert.ToInt32(r.Attribute("speed").Value)
                    }).ToList(),

                    Waves = x.Element("Waves").Elements("Wave").Select(y => new Wave()
                    {
                        Groups = y.Elements("Group").Select(r => new Group()
                        {
                            ID = Convert.ToInt32(r.Attribute("id").Value),
                            Count = Convert.ToInt32(r.Attribute("count").Value)
                        }).ToList()
                    }).ToList()
                }).First();
           }


        internal class Map
        {
            public List<StructuresList> Structures { set; get; }

            public List<EnemysList> Enemys { set; get; }

            public List<Wave> Waves { set; get; }
        }

        internal class StructuresList
        { 
            public string Name{set;get;}

            public int Attack { set; get; }

            public int I { set; get; }

            public int J { set; get; }

            public int Range { set; get; }

            public int Price { set; get; }
        }

        internal class EnemysList
        {

            public int ID { set; get; }
            public string Color { set; get; }
            public int HP { set; get; }

            public int  Orobyte { set; get; }
            public int Speed { set; get; }

        }

        internal class Wave
        {
            public int WaveID { set; get; }
            public List<Group> Groups { set; get; }
        }

        internal class Group
        {
            public int ID { set; get; }

            public int Count { set; get; }
        }

To print data out

             Console.WriteLine("Strucrures"); 
                foreach (var str in map.Structures)
                {
                    Console.WriteLine("Name :{0}  , Attack:{1}  , I:{2}  , J:{3}  ,  Range:{4}  ,  Price:{5}"
                        , str.Name, str.Attack, str.I, str.J, str.Range, str.Price);
                }


                Console.WriteLine("Enemys");
                foreach (var enmy in map.Enemys)
                {
                    Console.WriteLine("ID :{0}  , Color:{1}  , HP:{2}  , Orobyte:{3}  ,  Speed:{4} "
                        , enmy.ID, enmy.Color, enmy.HP, enmy.Orobyte, enmy.Speed);
                }


                Console.WriteLine("Waves");

                 foreach (var enmy in map.Waves)
                 {
                    Console.WriteLine("Wave ID:{0}", enmy.WaveID);
                    foreach(var gr in enmy.Groups)
                    Console.WriteLine("ID :{0}  , Count:{1} "
                        , gr.ID, gr.Count);
                  }
Alyafey
  • 1,455
  • 3
  • 15
  • 23