naghal naghal - 1 month ago 14
C# Question

xml to linq with two class c#

I am trying to read an XML document with linq and store it into a list of item. I have a class named "Item", which contain another class named "Effet". I am able to create an item, but not to have "Effet" initialized. (Either is stays null, or I get a compiler error if I try to write it different)
I tried multiple things, nothing worked. Here is what I tried. (Currently commented)

Here is my code:
Function that read XML

//Charge les items du jeu dans une liste<Item>
public static List<Item> chargerItems(string cheminFichier)
{
XElement xmlDoc = XElement.Load(cheminFichier);

List<Item> listeItem = (
from i in xmlDoc.Elements("item")
select new Item()
{
Nom = (string)i.Element("nom").Value,
Cout = int.Parse(i.Element("cout").Value),
Ville = int.Parse(i.Element("ville").Value),
Effet = (from eff in i.Element("effet").Elements("effet") select new Effet { Duree = eff.Value})
// {
// Cible = null,
//Categorie = (Categorie)Enum.Parse(typeof(Categorie), (string)i.Element("categorie"))//,
// Magnitude = (int)i.Element("magnitude"),
// Duree = (int)i.Element("duree")
// }
//(

// from eff in i.Elements("effet")
// select new Effet

// {
// //Cible = (Monstre) eff.Element("cible"),
// Categorie = (Categorie) Enum.Parse(typeof(Categorie), (string)eff.Element("categorie")),
// Magnitude = (int)eff.Element("magnitude"),
// Duree = (int)eff.Element("duree"),

// }
// ) as Effet
}


).ToList();

return listeItem;
}


My two classes:

namespace RPGProject.Entity
{
public class Effet
{
public Monstre Cible { get; set; }
public Categorie Categorie { get; set; }
public int Magnitude { get; set; }
public int Duree { get; set; }

public Effet()
{
//this.Cible = null;
//this.Categorie = Categorie.Divers;
this.Magnitude = 0;
this.Duree = 0;
}

}
}

namespace RPGProject
{
public enum Categorie
{
Guérison,
Résurrection,
Attaque,
Stimulant, //Gain en force, énergie, etc
Divers //Orbe, sphère de capture
}

public class Item
{
public string Nom { get; set; }
public Effet Effet { get; set; }
public int Cout { get; set; } //$$$
public int Ville { get; set; }
public int Rarete { get; set; }

public Item() {}
public Item(string nom) { this.Nom = nom; }

public Item(string nom, Effet effet, int cout, int ville)
{
this.Nom = nom;
this.Effet = new Effet();
this.Cout = cout;
this.Ville = ville;
}

//Redéfinition afin de pouvoir comparer les items du dictionnaire
public override bool Equals(object obj)
{
var k = obj as Item;
if (k != null)
{
return this.Nom == k.Nom;
}
return base.Equals(obj);
}

//Redéfinition afin de pouvoir comparer les items du dictionnaire
public override int GetHashCode()
{
return this.Nom.GetHashCode();
}
}
}


XML file

<liste_item>
<item>
<nom>Seringue d'adrénaline</nom>
<effet>
<cible></cible>
<categorie>Résurrection</categorie>
<magnitude>0</magnitude>
<duree>1</duree>
</effet>
<categorie></categorie>
<cout>250</cout>
<ville>1</ville>
</item>
</liste_item>

Answer

You are not that far off. Try making your LINQ query like this:

List<Item> listeItem = (
   from i in xmlDoc.Elements("item")
   let eff = i.Element("effet")
   select new Item()
   {
       Nom = i.Element("nom").Value,
       Cout = int.Parse(i.Element("cout").Value),
       Ville = int.Parse(i.Element("ville").Value),
       Effet = new Effet
       {
           Categorie = (Categorie)Enum.Parse(typeof(Categorie), eff.Element("categorie").Value),
           Magnitude = int.Parse(eff.Element("magnitude").Value),
           Duree = int.Parse(eff.Element("duree").Value)
       }
   }).ToList();