根据名称从xml文件加载pokemon

时间:2016-12-15 15:54:07

标签: c# .net xml

我有一个包含600+口袋妖怪的XML文件,每个口袋妖怪都有基本属性,当从下拉列表中选择口袋妖怪时,我想加载到口袋妖怪基础数据中。

XML文件如下:

<Pokemon>
   <Name>Bulbasaur</Name>

   <BaseStats>
     <Health>5</Health>
     <Attack>5</Attack>
     <Defense>5</Defense>
     <SpecialAttack>7</SpecialAttack>
     <SpecialDefense>7</SpecialDefense>
     <Speed>5</Speed>
   </BaseStats>
</Pokemon>
<Pokemon>
   <Name>Ivysaur</Name>

   <BaseStats>
     <Health>7</Health>
     <Attack>7</Attack>
     <Defense>6</Defense>
     <SpecialAttack>9</SpecialAttack>
     <SpecialDefense>8</SpecialDefense>
     <Speed>5</Speed>
   </BaseStats>
</Pokemon>

我的代码是:

XDocument pokemonDoc = XDocument.Load(@"c:\users\reece\documents\visual studio 2015\Projects\Pokesheet\Pokesheet\Files\pokemon.xml");

var pokemon = pokemonDoc.Descendants("Pokemon").Select(x => new
{
    name = (string)x.Element("Name"),
    health = (int)x.Element("BaseStats").Element("Health"),
    attack = (int)x.Element("BaseStats").Element("Attack"),
    defense = (int)x.Element("BaseStats").Element("Defense"),
    specialAttack = (int)x.Element("BaseStats").Element("SpecialAttack"),
    specialDefense = (int)x.Element("BaseStats").Element("SpecialDefense"),
    speed = (int)x.Element("BaseStats").Element("Speed"),
});

baseHp.Value = pokemon.health;
baseAttack.Value = pokemon.attack;
baseDefense.Value = pokemon.defense;
baseSpAttack.Value = pokemon.specialAttack;
baseSpDefense.Value = pokemon.specialDefense;
baseSpeed.Value = pokemon.speed;

我希望它只在xml文件中的名称与用户选择的名称匹配时才创建一个口袋妖怪,我该怎么做?

3 个答案:

答案 0 :(得分:2)

 var pokemon = pokemonDoc.Descendants("Pokemon").Select(x => new
    {

        name = (string)x.Element("Name"),
        health = (int)x.Element("BaseStats").Element("Health"),
        attack = (int)x.Element("BaseStats").Element("Attack"),
        defense = (int)x.Element("BaseStats").Element("Defense"),
        specialAttack = (int)x.Element("BaseStats").Element("SpecialAttack"),
        specialDefense = (int)x.Element("BaseStats").Element("SpecialDefense"),
        speed = (int)x.Element("BaseStats").Element("Speed"),

    }).Where(x => x.name == "yourName").FirstOrDefault();

这应该为您带来所选名称的宠物小精灵,如果它没有找到任何

,则为null

答案 1 :(得分:1)

这应该可以解决问题:

// Your user selected pokemon
string selectedPokemon = pokemonFromDropDownList.name; 

var pokemon = pokemonDoc.Descendants("Pokemon").Where(p => p.name == selectedPokemon).Select(x => new
{

    name = (string)x.Element("Name"),
    health = (int)x.Element("BaseStats").Element("Health"),
    attack = (int)x.Element("BaseStats").Element("Attack"),
    defense = (int)x.Element("BaseStats").Element("Defense"),
    specialAttack = (int)x.Element("BaseStats").Element("SpecialAttack"),
    specialDefense = (int)x.Element("BaseStats").Element("SpecialDefense"),
    speed = (int)x.Element("BaseStats").Element("Speed"),

});

    // Then do what ever you want with it
    if(pokemon != null) 
    {
        baseHp.Value = pokemon.health;
        baseAttack.Value = pokemon.attack;
        baseDefense.Value = pokemon.defense;
        baseSpAttack.Value = pokemon.specialAttack;
        baseSpDefense.Value = pokemon.specialDefense;
        baseSpeed.Value = pokemon.speed;
    }
    else
    {
        // Do stuff if it not exist...
    }

答案 2 :(得分:0)

谢谢大家,我使用了你的答案,但我们在where语句中将它们比作错误,完成的答案是:

var pokemon = pokemonDoc.Descendants("Pokemon").Select(x => new
        {

            name = (string)x.Element("Name"),
            health = (int)x.Element("BaseStats").Element("Health"),
            attack = (int)x.Element("BaseStats").Element("Attack"),
            defense = (int)x.Element("BaseStats").Element("Defense"),
            specialAttack = (int)x.Element("BaseStats").Element("SpecialAttack"),
            specialDefense = (int)x.Element("BaseStats").Element("SpecialDefense"),
            speed = (int)x.Element("BaseStats").Element("Speed"),

        }).Where(x => x.name.ToString() == cbSpecies.SelectedItem.ToString()).FirstOrDefault();

        baseHp.Value = pokemon.health;
        baseAttack.Value = pokemon.attack;
        baseDefense.Value = pokemon.defense;
        baseSpAttack.Value = pokemon.specialAttack;
        baseSpDefense.Value = pokemon.specialDefense;
        baseSpeed.Value = pokemon.speed;