0

I'm getting the following error:

Cannot implicitly convert type 'System.Collections.Generic.List>' to '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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user726720
  • 1,127
  • 7
  • 25
  • 59
  • It is simply because it is two different classes. If your Check class is supposed to hold the info from the xml then you have to store it in a new Check class then return that list. Currently you are trying to return a list. – deathismyfriend Oct 01 '14 at 05:48
  • can you give me an example please – user726720 Oct 01 '14 at 05:50

3 Answers3

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.

Dennis
  • 37,026
  • 10
  • 82
  • 150
1

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

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.

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

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);
}
Koenyn
  • 694
  • 4
  • 16
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

return listing.Select(x => new Check(x)).ToList(); 
JoriO
  • 1,050
  • 6
  • 13