我有一个带有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文件中的名称与用户选择的名称匹配时,我希望它只创建一个口袋妖怪,如何做到这一点?
发布于 2016-12-15 16:16:40
谢谢各位,我用了你们的答案,但是我们在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;发布于 2016-12-15 15:57:54
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。
发布于 2016-12-15 16:01:06
这应该能起作用:
// 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...
}https://stackoverflow.com/questions/41168239
复制相似问题