xmlnodelist to a list of objects in C#

I am reading an xml file using xmldocument. I extracted the nodes of xml using xmlnodelist. I would like to now assign the xmlnodelist to a list of objects "Project". Please let me know how I can do that .

<Projects>
  <Project>
    <Id>1</Id>
    <Name>Fabric1</Name>
  </Project>
   <Project>
    <Id>2</Id>
    <Name>Fabric2</Name>
  </Project>
</Projects>


Class :
 public class Project
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }


Code :

     XmlDocument xdoc = new XmlDocument();
                xdoc.Load(Server.MapPath("~/Content/abc.xml"));
                XmlNodeList listofProjests = xdoc.SelectNodes("/Projects/Project");
                var project = new List<Project>();

                foreach (XmlNode p in listofProjests)
                {
                    ?? = p.SelectSingleNode("Id").InnerText;
                    ?? = p.SelectSingleNode("Name").InnerText;
                 }
Jon Skeet
people
quotationmark

As I noted in a comment, you can do this trivially in LINQ to XML:

var projects = XDocument
    .Load(Server.MapPath("~/Content/abc.xml"))
    .Root
    .Elements("Project")
    .Select(p => new Project {
                Id = (string) p.Element("Id"),
                Name = (string) p.Element("Name")
            })
    .ToList();

I'd strongly advise you to use LINQ to XML rather than the old XmlDocument API if you can (and by now, you really should be able to... LINQ to XML has been around for a long time).

Of course, you could do it with your current code:

foreach (XmlNode p in listofProjests)
{
    string id = p.SelectSingleNode("Id").InnerText;
    string name = p.SelectSingleNode("Name").InnerText;
    project.Add(new Project { Id = id, Name = name });
}

or slightly more pleasantly (IMO):

foreach (XmlElement p in listofProjests)
{
    string id = p["Id"].InnerText;
    string name = p["Name"].InnerText;
    project.Add(new Project { Id = id, Name = name });
}

... but I really wouldn't.

people

See more on this question at Stackoverflow