Populate a List Collection using methods

janilane

Active member
Joined
Jan 23, 2008
Messages
30
Programming Experience
Beginner
Hello,

If I run my web service code, it generates a serialized XML with only one level, that is, ParentElement and ChildElement. My goal is to make it a multiple XML levels that even grandChild will show, as the sample output below. How can I achieve this using List Collection looping. Do I need to create several functions that returns List objects for each column of the dataset I have?

Thanks in advance of your help. Would appreciate a sample code too. This code snippet is in C#, but VB.net code is not a problem because I'm familiar with it.




public List<MyClassEntity> ExtractEmployee(...params here...)
{
...SQL CONNECTION HERE AND SP TO RUN THE QUERY...


while (reader.Read())
{
myList.Add(MapColumnsFromReader(reader));
}
return myList;
}


private myClassEntity MapColumnsFromReader (SqlDataReader reader)
{
return new MyClassEntity
{
EmployeeID = Convert.ToInt32(reader[EmployeeID]),
FirstName = reader[FirstName].ToString(),
FamilyName = reader[FamilyName].ToString(),
PastEmployerNames = reader[Employers].ToString(),
YearPositionHeld = Convert.ToInt32(reader[YearPos]),
RolesHeld = reader[Roles].ToString()

};
}

Dataset:

EmpID FirstName FamilyName PastEmployer YearPosHeld RolesHeld
123 John Adams Contoso Inc. 1994 Data Clerk
123 John Adams Contoso Inc. 1995 Analyst
123 John Adams Taj Software 1995 Senior Analyst
123 John Adams Taj Software 1995 Technical Specialist
324 Mikel Borfed MicroShell Corp 1989 Data Analyst
541 Greggy Dorsane Digital Media Ltd. 1998 Systems Analyst
541 Greggy Dorsane Digital Media Ltd. 1998 Senior Systems Analyst
541 Greggy Dorsane Digital Media Ltd. 1999 Supervisor
541 Greggy Dorsane Digital Media Ltd. 1999 Senior Supervisor
541 Greggy Dorsane Digital Media Ltd. 2001 Manager


DESIRED Serialized SOAP XML:

<Employees>
<employee>
<EmpId>123</EmpId>
<FirstName>John</FirstName>
<FamilyName>Adams</FamilyName>
<PastEmployers>
<EmployerName>Contoso Inc.</EmployerName>
<YearPosHeld>1994</YearPosHeld>
<RolesHeld>Data Clerk</RolesHeld>
<EmployerName>Contoso Inc.</EmployerName>
<YearPosHeld>1995</YearPosHeld>
<RolesHeld>Analyst</RolesHeld>
<EmployerName>Taj Software</EmployerName>
<YearPosHeld>1995</YearPosHeld>
<RolesHeld>SeniorAnalyst</RolesHeld>
<EmployerName>Taj Software</EmployerName>
<YearPosHeld>1995</YearPosHeld>
<RolesHeld>Technical Specialist</RolesHeld>
<PastEmployers>
</employee>
<employee>
<EmpId>541</EmpId>
<FirstName>Mikel</FirstName>
<FamilyName>Borfed</FamilyName>
<PastEmployers>
<EmployerName>Microshell Corp</EmployerName>
<YearPosHeld>1989</YearPosHeld>
<RolesHeld>Data Analyst</RolesHeld>
<PastEmployers>
</employee>
...other employees here...
...

</Employees>
 
Personally I'd drop the query into a datatable and parse the individual field objects of the rows collection to build your xml.

Possibly not the best way though - one of the other contributors here might know of a more direct way.
 
This code snippet is in C#, but VB.net code is not a problem because I'm familiar with it.
Also remember that this place is a VB.Net forums site, use VB.Net code when posting here. There is a similar C# forum here C# Developer Forums that you may use if that is more appropriate for your request.
 
Back
Top