无法隐式转换类型system.colllections.generic

时间:2022-11-25 16:30:56

I'm getting the following error:

我收到以下错误:

Cannot implicitly convert type 'System.Collections.Generic.List>' to 'System.Collections.Generic.List

无法将类型'System.Collections.Generic.List>'隐式转换为'System.Collections.Generic.List

The code I have is:

我的代码是:

class Check
{
      public string Text { get; set; }

      public  List<Check> getxml()
      {
          XDocument xdoc = XDocument.Load(@"D:\Web Utf-8 Converter\Categories.xml");

          var list = (from p in xdoc.Descendants("Categories")
                      select p.Elements("name"));

          var listing = list.ToList();

          return listing;
      }
}

This seems to me like a conversion error. But I'm not sure how to convert. Kindly assist.

在我看来,这似乎是一个转换错误。但我不知道如何转换。请帮助。

Thank you.

谢谢。

3 个解决方案

#1


1  

the select statement is returning a System.Xml.Linq.XElement. The return type of your method is List. These are not the same.

select语句返回System.Xml.Linq.XElement。方法的返回类型是List。这些都不一样。

what you can do is:

你能做的是:

 var list = (from p in xdoc.Descendants("Categories")
     select new Check { Text = p.Elements("name").Value});

However I think your class Check should not read the xml and store the results of each element.

但是我认为你的类Check不应该读取xml并存储每个元素的结果。

You should rather have a class "XmlParser" which has the GetXml Method and a class "XmlValue" which holds the information. i.e.

你应该有一个“XmlParser”类,它有GetXml方法和一个保存信息的类“XmlValue”。即

public class XmlValue
{
    public System.Xml.Linq.XElement Element { get; set; }
    public string Text
    {
        get
        {
            if(Element == null) return null;
            return Element.Value;
        }
    }
}

public class XmlParser
{
    public List<XmlValue> GetXml()
    {
        XDocument xdoc = XDocument.Load(@"D:\Web Utf-8 Converter\Categories.xml");

        return xdoc
            .Descendants("Categories")
            .SelectMany(p => p.Elements("name"))
            .Select(p => new XmlValue { Element = p });
            .ToList();
    }
}

void Main()
{
    XmlParser parser = new XmlParser();
    var list = parser.GetXml();
    foreach(var el in list)
        Console.WriteLine(el.Text);
}

#2


2  

According to your edits, it should be something like this:

根据你的编辑,它应该是这样的:

return xdoc
    .Descendants("Categories")
    .SelectMany(_ => _.Elements("name"))
    .Select(_ => new Check(_.Value))
    .ToList();

Note, that getxml shouldn't be an instance method of Check class. It should be at least static factory method.

注意,getxml不应该是Check类的实例方法。它至少应该是静态工厂方法。

#3


0  

You are returning a list coming from

您将返回一份来自的列表

var list = (from p in xdoc.Descendants("Categories")
                  select p.Elements("name"));

But the return type is List. A 'System.Xml.Linq.XElement' is not a 'Check', you must somehow provide the conversion. Maybe write a constructor to Check-class accepting System.Xml.Linq.XElement and then instead of returning listing, you can

但返回类型是List。 'System.Xml.Linq.XElement'不是'Check',你必须以某种方式提供转换。也许写一个构造函数给Check-class接受System.Xml.Linq.XElement,然后代替返回列表,你可以

return listing.Select(x => new Check(x)).ToList(); 

#1


1  

the select statement is returning a System.Xml.Linq.XElement. The return type of your method is List. These are not the same.

select语句返回System.Xml.Linq.XElement。方法的返回类型是List。这些都不一样。

what you can do is:

你能做的是:

 var list = (from p in xdoc.Descendants("Categories")
     select new Check { Text = p.Elements("name").Value});

However I think your class Check should not read the xml and store the results of each element.

但是我认为你的类Check不应该读取xml并存储每个元素的结果。

You should rather have a class "XmlParser" which has the GetXml Method and a class "XmlValue" which holds the information. i.e.

你应该有一个“XmlParser”类,它有GetXml方法和一个保存信息的类“XmlValue”。即

public class XmlValue
{
    public System.Xml.Linq.XElement Element { get; set; }
    public string Text
    {
        get
        {
            if(Element == null) return null;
            return Element.Value;
        }
    }
}

public class XmlParser
{
    public List<XmlValue> GetXml()
    {
        XDocument xdoc = XDocument.Load(@"D:\Web Utf-8 Converter\Categories.xml");

        return xdoc
            .Descendants("Categories")
            .SelectMany(p => p.Elements("name"))
            .Select(p => new XmlValue { Element = p });
            .ToList();
    }
}

void Main()
{
    XmlParser parser = new XmlParser();
    var list = parser.GetXml();
    foreach(var el in list)
        Console.WriteLine(el.Text);
}

#2


2  

According to your edits, it should be something like this:

根据你的编辑,它应该是这样的:

return xdoc
    .Descendants("Categories")
    .SelectMany(_ => _.Elements("name"))
    .Select(_ => new Check(_.Value))
    .ToList();

Note, that getxml shouldn't be an instance method of Check class. It should be at least static factory method.

注意,getxml不应该是Check类的实例方法。它至少应该是静态工厂方法。

#3


0  

You are returning a list coming from

您将返回一份来自的列表

var list = (from p in xdoc.Descendants("Categories")
                  select p.Elements("name"));

But the return type is List. A 'System.Xml.Linq.XElement' is not a 'Check', you must somehow provide the conversion. Maybe write a constructor to Check-class accepting System.Xml.Linq.XElement and then instead of returning listing, you can

但返回类型是List。 'System.Xml.Linq.XElement'不是'Check',你必须以某种方式提供转换。也许写一个构造函数给Check-class接受System.Xml.Linq.XElement,然后代替返回列表,你可以

return listing.Select(x => new Check(x)).ToList();