首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于名称从xml文件中加载pokemon

基于名称从xml文件中加载pokemon
EN

Stack Overflow用户
提问于 2016-12-15 15:54:06
回答 3查看 321关注 0票数 0

我有一个带有600+口袋妖怪的XML文件,每个口袋妖怪都有基本的统计数据,当从我想要加载到口袋妖怪基本状态的下拉列表中选择一个口袋妖怪时。

XML文件如下:

代码语言:javascript
复制
<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>

我的代码是:

代码语言:javascript
复制
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文件中的名称与用户选择的名称匹配时,我希望它只创建一个口袋妖怪,如何做到这一点?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-12-15 16:16:40

谢谢各位,我用了你们的答案,但是我们在where语句中比较错误,最终的答案是:

代码语言:javascript
复制
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;
票数 0
EN

Stack Overflow用户

发布于 2016-12-15 15:57:54

代码语言:javascript
复制
 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。

票数 2
EN

Stack Overflow用户

发布于 2016-12-15 16:01:06

这应该能起作用:

代码语言:javascript
复制
// 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...
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41168239

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档