Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
<Activities>
  <Activity>
    <ActivityName>Projekt</ActivityName>
    <ProjectNr>001</ProjectNr>
  </Activity>
  <Activity>
    <ActivityName>Absence</ActivityName>
    <ProjectNr>000</ProjectNr>
  </Activity>
</Activities>


Code in Controller:

C#
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. :-(
Posted
Updated 12-Apr-17 2:23am
Comments
CHill60 12-Apr-17 6:39am    
Try moving everything other than the update to the list outside of the loop i.e.
List<string> list = new List<string>();
foreach (var ProjNrList in Activity)
{
   list.Add(ProjNrList.Element("ProjectNr").Value);
}
ViewBag.ProjNrList = list;
return View();
Member 13123379 12-Apr-17 8:22am    
Thank you so much! I knew it was something simple... ;-) It works now.
[no name] 12-Apr-17 8:11am    
You are getting only last record because in for loop you are creating List<string> list = new List<string>(); every time you iterate.

1 solution

That's what the problem was and I know why I got tricked by it too since I have tried to have things outside the loop, but not exactly the way you did it. ;-)
Great! Thanks!

C#
<pre>List<string> list = new List<string>();
foreach (var ProjNrList in Activity)
{
   list.Add(ProjNrList.Element("ProjectNr").Value);
}
ViewBag.ProjNrList = list;
return View();
 
Share this answer
 
Comments
CHill60 12-Apr-17 8:33am    
You might have given me the opportunity to post the solution. Next time let the member who helps you in the comments post the solution that works.
Member 13123379 12-Apr-17 8:37am    
So sorry! Thought I was supposed to do that. My appologies. Won't happen again. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900