Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.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

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# 使用linq搜索Xml文档不会返回任何内容_C#_Xml_Linq - Fatal编程技术网

C# 使用linq搜索Xml文档不会返回任何内容

C# 使用linq搜索Xml文档不会返回任何内容,c#,xml,linq,C#,Xml,Linq,我正在尝试学习如何用XML在C中保存和提取数据,尽管我已经阅读了关于这里类似问题的各种答案,但我无法理解为什么我的语句没有返回任何内容。 我正在编写的程序只是一个测试,在这个测试中,我在xml文档中保存了几部电影的数据,现在我正试图根据成本检索其中的一些电影 这是我为搜索而编写的类: class ExtractData { private XDocument _xdoc; public List<string> SearchByCost(double cost)

我正在尝试学习如何用XML在C中保存和提取数据,尽管我已经阅读了关于这里类似问题的各种答案,但我无法理解为什么我的语句没有返回任何内容。 我正在编写的程序只是一个测试,在这个测试中,我在xml文档中保存了几部电影的数据,现在我正试图根据成本检索其中的一些电影

这是我为搜索而编写的类:

 class ExtractData
{
    private XDocument _xdoc;
    public List<string> SearchByCost(double cost)
    {
        _xdoc = XDocument.Load(FileLocation.XmlFileLocation);
        List<string> list = new List<string>();

        var movies = from movie in _xdoc.Root.Elements("Name")
                where Convert.ToDouble(movie.Element("Cost").Value) < cost
                select movie;

        foreach (var item in movies)
        {
            list.Add(item.Value);
        }

        return list;
    }

}
这是XML文档的内容:

<?xml version="1.0" encoding="utf-8"?>
<Movies>
  <Movie>
    <Id>1</Id>
    <Name>Shawshank Redemption</Name>
    <Director>Frank Darabont</Director>
    <Year>1994</Year>
    <Cost>9.95</Cost>
  </Movie>
  <Movie>
    <Id>2</Id>
    <Name>Pulp Fiction</Name>
    <Director>Quentin Tarantino</Director>
    <Year>1995</Year>
    <Cost>8.95</Cost>
  </Movie>
  <Movie>
    <Id>3</Id>
    <Name>Sharknado</Name>
    <Director>Anthony Ferrante</Director>
    <Year>2013</Year>
    <Cost>5.95</Cost>
  </Movie>
</Movies>
我希望这是足够的信息来尝试帮助我,并提前感谢!:

根元素包含电影元素:

var movies = from movie in _xdoc.Root.Elements("Movie")  // here instead of "Name"
             where (double)movie.Element("Cost") < cost
             select movie;

用通俗易懂的英语说,你似乎在努力寻找比9美元更便宜的电影

您可以这样写:

公共IReadOnlyCollection SearchByCostXDocument xdoc,双倍成本 { 返回xdoc.Root.ElementsMovie .Wheremovie=>doublemovie.element成本<成本 .Selectmovie=>movie.ElementName.Value 托利斯特先生; }
您需要调试代码。此外,var movies=from movie in_xdoc.Root.elements name如果选择它,可能不会返回任何内容,但如果它返回了。。。所有元素。然后尝试通过movie.ElementCost检索成本,但该元素没有任何子元素!!至少,您需要将其更改为xdoc.Root.ElementsMovie,并在最后执行select movie.ElementsName或类似操作,以获得所有匹配电影的名称。谢谢,我显然在linq查询中把元素名称弄混了。感谢您的帮助:哈哈,是的,没错——在现实生活中的应用程序中,货币和其他东西当然会被指定,但这只是为了掌握xml的诀窍。事实上,我不仅使用了名称,还使用了返回电影列表的方法:哎呀,代码格式在注释中不起作用:D,但是的,它现在可以按预期工作了!谢谢你的帮助:!你太好了,没有时间回答这个问题。
var movies = from movie in _xdoc.Root.Elements("Movie")  // here instead of "Name"
             where (double)movie.Element("Cost") < cost
             select movie;
List<string> list = movies.Select(m => (string)m.Element("Name")).ToList();