title: How to foreach simplefield and simpledata using SharpKML in ASP.NET web API?
body: <p>I have problem with foreach the <code>&lt;SimpleField&gt;</code> and <code>&lt;SimpleData&gt;</code> tags, how can I foreach them?</p>
  
  <p>I've tried loop it but is did not working, I using sharpKML because the code before using this library for generate the kml file</p>
  
  <p>I have code for looping the data from database to add it into xml file using SharpKML, and I generate it to a file in controller</p>
  
  <p>this is my code</p>
  
  <pre><code>public static async Task&lt;object&gt; ExecuteTest(string query)
              {
                  string connStrResult = ConfigurationManager.ConnectionStrings["PostGresConnection"].ConnectionString;
                  NpgsqlConnection connection;
                  NpgsqlCommand command;
                  NpgsqlDataReader reader;
  
              List&lt;PointMaster&gt; master = new List&lt;PointMaster&gt;();
              List&lt;AttDetailCable&gt; cable = new List&lt;AttDetailCable&gt;();
  
              connection = new NpgsqlConnection(connStrResult);
              connection.Open();
              command = new NpgsqlCommand(query, connection);
              reader = command.ExecuteReader();
  
              LineStyle lineStyle = new LineStyle();
              lineStyle.Color = Color32.Parse("ff0000ff");
  
  
              Style trackStyle = new Style();
              trackStyle.Id = "StyleLine";
              trackStyle.Line = lineStyle;
  
              var doc = new Document();
              doc.Id = "root_doc";
  
              var kml = new Kml();
  
              var folder = new Folder();
              folder.Name = "Download_Spasial_Kabel";
  
              while (await reader.ReadAsync())
                  {
                  AttDetailCable cables = new AttDetailCable
                  {
                      oid = (uint)reader["oid"],
                      system_id = (string)reader["system_id"],
                      cable_id = (string)(reader["cable_id"]),
                      cable_name = (string)reader["cable_name"],
                      parent_id = (string)reader["parent_id"],
                      parent_type = (string)reader["parent_type"],
                      region_name = (string)reader["region_name"],
                      province_name = (string)reader["province_name"],
                      a_location = (string)reader["a_location"],
                      b_location = (string)reader["b_location"],
                      total_core = (int)reader["total_core"],
                      no_of_tube = (int)reader["no_of_tube"],
                      no_of_core_per_tube = (int)reader["no_of_core_per_tube"],
                      cable_measured_length = (double)reader["cable_measured_length"],
                      cable_calculated_length = (double)reader["cable_calculated_length"],
                      overheadunderground = (string)reader["overheadunderground"],
                      total_no_chamber_splice = (int)reader["total_no_chamber_splice"],
                      coreaccess = (string)reader["coreaccess"],
                      wavelength = (string)reader["wavelength"],
                      optical_output_power = (string)reader["optical_output_power"],
                      frequency = (string)reader["frequency"],
                      attenuation_db = (string)reader["attenuation_db"],
                      resistance_ohm = (string)reader["resistance_ohm"],
                      construction_stage = (string)reader["construction_stage"],
                      activation_stage = (string)reader["activation_stage"],
                      accessibility = (string)reader["accessibility"],
                      owner_name = (string)reader["owner_name"],
                      address = (string)reader["address"],
                      mobile_number = (string)reader["mobile_number"],
                      fax = (string)reader["fax"],
                      email_id = (string)reader["email_id"],
                      installation_number = (string)reader["installation_number"],
                      installation_year = (int)reader["installation_year"],
                      production_year = (int)reader["production_year"],
                      installation_company = (string)reader["installation_company"],
                      installation_technician = (string)reader["installation_technician"],
                      installation_date = (DateTime)reader["installation_date"],
                      user_remark = (string)reader["user_remark"],
                      status = (string)reader["status"],
                      user_id = (int)reader["user_id"],
                      cable_brand = (string)reader["cable_brand"],
                      cable_model = (string)reader["cable_model"],
                      hierarchy_type          = reader["hierarchy_type"] == DBNull.Value ? string.Empty : reader["hierarchy_type"].ToString(),
                      r01_region_id = (string)reader["r01_region_id"],
                      r02_data_pole = (string)reader["r02_data_pole"],
                      r03_data_grade = (string)reader["r03_data_grade"],
                      r04_label = (string)reader["r04_label"],
                      remark_5 = (string)reader["remark_5"],
                      remark_6 = (string)reader["remark_6"],
                      remark_8 = (string)reader["remark_8"],
                      remark_7 = (string)reader["remark_7"],
                      r9_asset_no = (string)reader["r9_asset_no"],
                      r10_band_meter_a_z_m_ = (string)reader["r10_band_meter_a_z_m_"],
                      remark_12 = (string)reader["remark_12"],
                      r13_opex_name = (string)reader["r13_opex_name"],
                      remark_14 = (string)reader["remark_14"],
                      r15_project_name = (string)reader["r15_project_name"],
                      created_date = (DateTime)reader["created_date"],
                      sap_materialdesc = reader["sap_materialdesc"] == DBNull.Value ? string.Empty : reader["sap_materialdesc"].ToString(),
                      sap_serialnumber = reader["sap_serialnumber"] == DBNull.Value ? string.Empty : reader["sap_serialnumber"].ToString(),
                      sap_materialnumber = reader["sap_materialnumber"] == DBNull.Value ? string.Empty : reader["sap_materialnumber"].ToString(),
                      msid = (int)reader["msid"],
                      sp_geometry             = (PostgisLineString)reader["sp_geometry"],
                  };
  
  
                  var line = new LineString();
                  var coordinate = new CoordinateCollection();
                  foreach (var dimens in cables.sp_geometry)
                  {
                      coordinate.Add(new Vector(dimens.Y, dimens.X));
                      line.Coordinates = coordinate;
                  }
  
                  var placemarks = new Placemark();
                  placemarks.Name = reader["cable_name"].ToString();
                  placemarks.Geometry = line;
                  doc.AddFeature(placemarks); 
              }
  
              doc.AddStyle(trackStyle);
              kml.Feature = doc;
              Serializer serializer = new Serializer();
              serializer.Serialize(kml);
  
              return serializer.Xml;
  
              }
  </code></pre>
  
  <p>This is the output I want for this problem :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;kml xmlns="http://www.opengis.net/kml/2.2"&gt;
  &lt;Document id="root_doc"&gt;
  &lt;Schema name="Download_Spasial_Kabel" id="Download_Spasial_Kabel"&gt;
      &lt;SimpleField name="oid" type="int"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="system_id" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="cable_id" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="cable_name" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="parent_id" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="parent_type" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="region_name" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="province_name" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="a_location" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="b_location" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="total_core" type="int"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="no_of_tube" type="int"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="no_of_core_per_tube" type="int"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="cable_measured_length" type="float"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="cable_calculated_length" type="float"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="overheadunderground" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="total_no_chamber_splice" type="int"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="coreaccess" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="wavelength" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="optical_output_power" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="frequency" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="attenuation_db" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="resistance_ohm" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="construction_stage" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="activation_stage" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="accessibility" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="owner_name" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="address" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="mobile_number" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="fax" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="email_id" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="installation_number" type="int"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="installation_year" type="int"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="production_year" type="int"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="installation_company" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="installation_technician" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="installation_date" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="user_remark" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="status" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="user_id" type="int"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="user_name" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="type" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="cable_brand" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="cable_model" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="hierarchy_type" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="r01_region_id" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="r02_data_pole" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="r03_data_grade" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="r04_label" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="remark_5" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="remark_6" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="remark_7" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="remark_8" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="r9_asset_no" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="r10_band_meter_a_z_m_" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="remark_11" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="remark_12" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="r13_opex_name" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="remark_14" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="r15_project_name" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="created_date" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="sap_materialdesc" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="sap_serialnumber" type="int"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="sap_materialnumber" type="string"&gt;&lt;/SimpleField&gt;
      &lt;SimpleField name="msid" type="int"&gt;&lt;/SimpleField&gt;
  &lt;/Schema&gt;
  &lt;Folder&gt;&lt;name&gt;Download_Spasial_Kabel&lt;/name&gt;
    &lt;Placemark&gt;
      &lt;Style&gt;&lt;LineStyle&gt;&lt;color&gt;ff0000ff&lt;/color&gt;&lt;/LineStyle&gt;&lt;PolyStyle&gt;&lt;fill&gt;0&lt;/fill&gt;&lt;/PolyStyle&gt;&lt;/Style&gt;
      &lt;ExtendedData&gt;&lt;SchemaData &gt;
          &lt;SimpleData name="oid"&gt;54812580&lt;/SimpleData&gt;
          &lt;SimpleData name="system_id"&gt;CABL00000458488&lt;/SimpleData&gt;
          &lt;SimpleData name="cable_id"&gt;CABL_1DPR003_ODF01_DPRJ592_01&lt;/SimpleData&gt;
          &lt;SimpleData name="cable_name"&gt;ODF_1DPR003_ODF01-SPLC_DPRJ592&lt;/SimpleData&gt;
          &lt;SimpleData name="parent_id"&gt;OVH-Cable&lt;/SimpleData&gt;
          &lt;SimpleData name="parent_type"&gt;Pole&lt;/SimpleData&gt;schemaUrl="#Download_Spasial_Kabel"
          &lt;SimpleData name="region_name"&gt;RBNT&lt;/SimpleData&gt;
          &lt;SimpleData name="province_name"&gt;BALI&lt;/SimpleData&gt;
          &lt;SimpleData name="a_location"&gt;ODF_1DPR003_ODF01&lt;/SimpleData&gt;
          &lt;SimpleData name="b_location"&gt;SPLC_DPRJ592&lt;/SimpleData&gt;
          &lt;SimpleData name="total_core"&gt;12&lt;/SimpleData&gt;
          &lt;SimpleData name="no_of_tube"&gt;2&lt;/SimpleData&gt;
          &lt;SimpleData name="no_of_core_per_tube"&gt;6&lt;/SimpleData&gt;
          &lt;SimpleData name="cable_measured_length"&gt;2704.169&lt;/SimpleData&gt;
          &lt;SimpleData name="cable_calculated_length"&gt;2704.169&lt;/SimpleData&gt;
          &lt;SimpleData name="overheadunderground"&gt;Overhead&lt;/SimpleData&gt;
          &lt;SimpleData name="total_no_chamber_splice"&gt;0&lt;/SimpleData&gt;
          &lt;SimpleData name="coreaccess"&gt;Core&lt;/SimpleData&gt;
          &lt;SimpleData name="construction_stage"&gt;Built&lt;/SimpleData&gt;
          &lt;SimpleData name="activation_stage"&gt;Activated&lt;/SimpleData&gt;
          &lt;SimpleData name="accessibility"&gt;Yes&lt;/SimpleData&gt;
          &lt;SimpleData name="installation_year"&gt;2010&lt;/SimpleData&gt;
          &lt;SimpleData name="production_year"&gt;0&lt;/SimpleData&gt;
          &lt;SimpleData name="installation_company"&gt;PT. BANGUN PRIMA SEMESTA&lt;/SimpleData&gt;
          &lt;SimpleData name="installation_date"&gt;29/05/2015&lt;/SimpleData&gt;
          &lt;SimpleData name="user_remark"&gt;DATAENTRY_IMAM&lt;/SimpleData&gt;
          &lt;SimpleData name="status"&gt;A&lt;/SimpleData&gt;
          &lt;SimpleData name="user_id"&gt;80479&lt;/SimpleData&gt;
          &lt;SimpleData name="user_name"&gt;joko.siswanto&lt;/SimpleData&gt;
          &lt;SimpleData name="type"&gt;ADSS&lt;/SimpleData&gt;
          &lt;SimpleData name="r01_region_id"&gt;RBNT&lt;/SimpleData&gt;
          &lt;SimpleData name="r15_project_name"&gt;GI PESANGGARAN-GI NUSA DUA_DWDM&lt;/SimpleData&gt;
          &lt;SimpleData name="created_date"&gt;21/02/2018 12:23&lt;/SimpleData&gt;
          &lt;SimpleData name="sap_serialnumber"&gt;0&lt;/SimpleData&gt;
          &lt;SimpleData name="msid"&gt;17191&lt;/SimpleData&gt;
      &lt;/SchemaData&gt;&lt;/ExtendedData&gt;
        &lt;LineString&gt;&lt;coordinates&gt;115.210146853828,-8.71815797576044 115.21115,-8.718783 115.210983,-8.718917 115.210783,-8.719083 115.210483,-8.71915 115.210083,-8.719317 115.2096,-8.719583 115.208833,-8.719817 115.2082,-8.7201 115.207417,-8.720583 115.206433,-8.720883 115.20575,-8.7209 115.204933,-8.721067 115.204267,-8.72135 115.203517,-8.721583 115.20275,-8.721733 115.20185,-8.721917 115.201,-8.722167 115.20005,-8.7224 115.1992,-8.722567 115.198167,-8.7227 115.197383,-8.7228 115.1965,-8.722883 115.195717,-8.722933 115.194767,-8.72285 115.193917,-8.722883 115.193017,-8.722967 115.1921,-8.72295 115.191167,-8.72285 115.189883,-8.722833 115.18855,-8.723117&lt;/coordinates&gt;&lt;/LineString&gt;
    &lt;/Placemark&gt;
    &lt;/Folder&gt;
    &lt;/Document&gt;
  &lt;/kml&gt;
  </code></pre>