Nested foreach loops to get info from nested lists using C#



An object EmployeeInfoDocument contains a list of groups. Each group holds a list of employees. I am trying to use a nested foreach loop to write the values out to an XML file. It seems that I have the syntax wrong on the inner foreach loop. Visual Studio is giving me the following error:


foreach statement cannot operate on variables of type 'InfoObjects.Group' because 'InfoObjects.Group' does not contain a public definition for 'GetEnumerator'


How do I pull info from each employee within the groups? I'm extremly new to C#.


Any insight would be appreciated.



namespace InfoObjects
{
public class EmployeeInfoDocument
{
private List<Group> _groups = new List<Group>();
public List<Group> Groups
{
get { return _groups; }
set { _groups = value; }
}
}
public class Group
{
private string _text = string.Empty;
private List<Employee> _info = new List<Employee>
public Group()
{
}
public string Text
{
get {return _text; }
set { _text = value; }
}
public List<Employee> Employees
{
get { return _info }
set { _info = value; }
}
}

public class Employee
{
private string _name = string.Empty
private string _department = string.Empty
public Employee()
{
}
public string Name
{
get { return _name;}
set { _name = value;}
}
{
public string Department
{
get { return _department};
set { _department = value;}
}
}
}


namespace UI
{
public partial class Mainform: Form
{
private void OnSave(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.RestoreDirectory = false;
saveFileDialog.InitialDirectory = Assembly.GetExecutingAssembly().Location;
saveFileDialog.Filter = "Employee Files|*.xml";
saveFileDialog.DefaultExt = "xml";
using (saveFileDialog)
{
if (saveFileDialog.ShowDialog(this) != DialogResult.OK)
return;
}

using (XmlWriter xmlw = XmlWriter.Create(saveFileDialog.FileName))
{
EmployeeInfoDocument k = new EmployeeInfoDocument();
List<Group> myList = k.Groups;
xmlw.WriteStartDocument();
xmlw.WriteStartElement("EmployeeInfo");
foreach (Group group in EmployeeInfoMgr.Document.Groups)
{
xmlw.WriteStartElement("Group Name")
xmlw.WriteString(group.Text)
foreach (Employee employee in group)
{
xmlw.WriteStartElement("Employee Name");
xmlw.WriteString(employee.Name);
xmlw.WriteEndElement();
xmlw.WriteStartElement("Employee Department");
xmlw.WriteString(employee.Department);
xmlw.WriteEndElement();
}
xmlw.WriteEndElement()
}
xmlw.WriteEndElement();
xmlw.WriteEndDocument();
}
}
}
}


Thanks again. I've been stuck on this for hours now.


No comments:

Post a Comment