1
Answer

Looping in a dataset from SQLDataReader for a web service.

Gee Batol

Gee Batol

12y
1.5k
1
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.



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>


 
 


Answers (1)