Argument1: cannot convert from 'string' to 'int' error in List

asp.net-mvc, c#, xml

Solution

Your list is `List<int>` and you are trying to add a string value to your `List`, you can't do that.

You can parse the string to `int` using `int.Parse` or `Convert.ToInt32` or safely using `int.TryParse`

If your `Value` contains integer value then you can explicitly cast it like:

list.Add((int) node.Attributes["name"].Value);

or you can use:

list.Add(Convert.ToInt32(node.Attributes["name"].Value));

Problem

I have the following code ``` public static List<int> GetAllYear() { XmlDocument document = new XmlDocument(); document.Load(strXmlPath); XmlNodeList nodeList = document.SelectNodes("Year"); List<int> list = new List<int>(); foreach (XmlNode node in nodeList) { list.Add(node.Attributes["name"].Value.ToString()); //This line throws error } return list; } ``` when I try to build the solution I get the following error: ``` Argument1: cannot convert from 'string' to 'int' ``` Honestly I do not know why because when I return the result to the list variable I use the ToString() to convert it explicitly. Could someone help me understand what is going on here. I can post more code if needed. I have tried to just google the error message and it seems to be a generic error message but no one really explains the reason for the error. Thank you in advance

Original source