Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从Xml文件加载_C#_Xml_Linq To Xml - Fatal编程技术网

C# 从Xml文件加载

C# 从Xml文件加载,c#,xml,linq-to-xml,C#,Xml,Linq To Xml,好的,这是非常基本的,但我现在已经开始学习如何阅读XML文档,我通常会在这里找到比在线指南更全面的答案。基本上,我正在编写一个口袋妖怪游戏,它使用一个XML文件加载所有统计数据(我从其他人那里偷来的数据)。用户将输入一个口袋妖怪,然后我想从XML文件中读取口袋妖怪的基本统计数据,以提供一个模板,这将是一个口袋妖怪: <Pokemon> <Name>Bulbasaur</Name> <BaseStats> <Health

好的,这是非常基本的,但我现在已经开始学习如何阅读XML文档,我通常会在这里找到比在线指南更全面的答案。基本上,我正在编写一个口袋妖怪游戏,它使用一个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>
但这只会将pokemonDoc返回为null,知道我哪里出错了吗

注意:

cbSpeciesSelect是用户选择想要哪种口袋妖怪的地方

文件路径肯定可以工作,因为我已经在我的程序中使用了它


while循环从未真正启动

您可以尝试以下代码:

foreach(var e in pokemonDoc.Descendants("Pokemon").Elements("Name"))
{
    if(e.Value==cbSpecies.SelectedText)
    {
        var Stats = pokemonDoc.Descendants("Pokemon").Elements("BaseStats");
    }
}
试试xml linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var pokemon = doc.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"),
            }).FirstOrDefault();
        }
    }
}

您的xml文件中是否有
xml声明
?我不太确定这是什么,xml文件的顶部显示:
第一步直接序列化/反序列化到对象可能会更干净。()您确认文件路径正确吗…可以尝试绝对路径来确定吗?文件路径是绝对的,我只是没有把它和代码一起发布,它绝对有效相信我
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var pokemon = doc.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"),
            }).FirstOrDefault();
        }
    }
}