I have a XML file with some data and want to retrieve all elements by the tag name of "ProjectNr" in Controller and then send the list to View showing the project numbers in a dropdownlist.
The problem with below code is that it only shows the last element of "ProjectNr" which means it overwrites each element until it reaches the end of the loop and then only the last remains.
I have tried to make it save all the elements but nothing seems to be working. I am fairly new to C# so I am sure Im missing something simple or doing something completely wrong. ;-)
Does this make any sense?
The XML tree looks like this:
<Activities>
<Activity>
<ActivityName>Projekt</ActivityName>
<ProjectNr>001</ProjectNr>
</Activity>
<Activity>
<ActivityName>Absence</ActivityName>
<ProjectNr>000</ProjectNr>
</Activity>
</Activities>
Code in Controller:
public ActionResult NewActivity()
{
XElement xelement = XElement.Load(Server.MapPath("/App_Data/ActivitiesData.xml"));
IEnumerable<XElement> Activity = xelement.Elements();
foreach (var ProjNrList in Activity)
{
List<string> list = new List<string>();
list.Add(ProjNrList.Element("ProjectNr").Value);
ViewBag.ProjNrList = list;
continue;
}
return View();
}
What I have tried:
I think I have tried everything, but obviously I have not. ;-)
I have tried multible loops, linq, readXML and many different ways of retrieving the list. Do not know what to try next. :-(