How to foreach simplefield and simpledata using SharpKML in ASP.NET web API?

457 views Asked by At

I have problem with foreach the <SimpleField> and <SimpleData> tags, how can I foreach them?

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

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

this is my code

public static async Task<object> ExecuteTest(string query)
            {
                string connStrResult = ConfigurationManager.ConnectionStrings["PostGresConnection"].ConnectionString;
                NpgsqlConnection connection;
                NpgsqlCommand command;
                NpgsqlDataReader reader;

            List<PointMaster> master = new List<PointMaster>();
            List<AttDetailCable> cable = new List<AttDetailCable>();

            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;

            }

This is the output I want for this problem :

<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document id="root_doc">
<Schema name="Download_Spasial_Kabel" id="Download_Spasial_Kabel">
    <SimpleField name="oid" type="int"></SimpleField>
    <SimpleField name="system_id" type="string"></SimpleField>
    <SimpleField name="cable_id" type="string"></SimpleField>
    <SimpleField name="cable_name" type="string"></SimpleField>
    <SimpleField name="parent_id" type="string"></SimpleField>
    <SimpleField name="parent_type" type="string"></SimpleField>
    <SimpleField name="region_name" type="string"></SimpleField>
    <SimpleField name="province_name" type="string"></SimpleField>
    <SimpleField name="a_location" type="string"></SimpleField>
    <SimpleField name="b_location" type="string"></SimpleField>
    <SimpleField name="total_core" type="int"></SimpleField>
    <SimpleField name="no_of_tube" type="int"></SimpleField>
    <SimpleField name="no_of_core_per_tube" type="int"></SimpleField>
    <SimpleField name="cable_measured_length" type="float"></SimpleField>
    <SimpleField name="cable_calculated_length" type="float"></SimpleField>
    <SimpleField name="overheadunderground" type="string"></SimpleField>
    <SimpleField name="total_no_chamber_splice" type="int"></SimpleField>
    <SimpleField name="coreaccess" type="string"></SimpleField>
    <SimpleField name="wavelength" type="string"></SimpleField>
    <SimpleField name="optical_output_power" type="string"></SimpleField>
    <SimpleField name="frequency" type="string"></SimpleField>
    <SimpleField name="attenuation_db" type="string"></SimpleField>
    <SimpleField name="resistance_ohm" type="string"></SimpleField>
    <SimpleField name="construction_stage" type="string"></SimpleField>
    <SimpleField name="activation_stage" type="string"></SimpleField>
    <SimpleField name="accessibility" type="string"></SimpleField>
    <SimpleField name="owner_name" type="string"></SimpleField>
    <SimpleField name="address" type="string"></SimpleField>
    <SimpleField name="mobile_number" type="string"></SimpleField>
    <SimpleField name="fax" type="string"></SimpleField>
    <SimpleField name="email_id" type="string"></SimpleField>
    <SimpleField name="installation_number" type="int"></SimpleField>
    <SimpleField name="installation_year" type="int"></SimpleField>
    <SimpleField name="production_year" type="int"></SimpleField>
    <SimpleField name="installation_company" type="string"></SimpleField>
    <SimpleField name="installation_technician" type="string"></SimpleField>
    <SimpleField name="installation_date" type="string"></SimpleField>
    <SimpleField name="user_remark" type="string"></SimpleField>
    <SimpleField name="status" type="string"></SimpleField>
    <SimpleField name="user_id" type="int"></SimpleField>
    <SimpleField name="user_name" type="string"></SimpleField>
    <SimpleField name="type" type="string"></SimpleField>
    <SimpleField name="cable_brand" type="string"></SimpleField>
    <SimpleField name="cable_model" type="string"></SimpleField>
    <SimpleField name="hierarchy_type" type="string"></SimpleField>
    <SimpleField name="r01_region_id" type="string"></SimpleField>
    <SimpleField name="r02_data_pole" type="string"></SimpleField>
    <SimpleField name="r03_data_grade" type="string"></SimpleField>
    <SimpleField name="r04_label" type="string"></SimpleField>
    <SimpleField name="remark_5" type="string"></SimpleField>
    <SimpleField name="remark_6" type="string"></SimpleField>
    <SimpleField name="remark_7" type="string"></SimpleField>
    <SimpleField name="remark_8" type="string"></SimpleField>
    <SimpleField name="r9_asset_no" type="string"></SimpleField>
    <SimpleField name="r10_band_meter_a_z_m_" type="string"></SimpleField>
    <SimpleField name="remark_11" type="string"></SimpleField>
    <SimpleField name="remark_12" type="string"></SimpleField>
    <SimpleField name="r13_opex_name" type="string"></SimpleField>
    <SimpleField name="remark_14" type="string"></SimpleField>
    <SimpleField name="r15_project_name" type="string"></SimpleField>
    <SimpleField name="created_date" type="string"></SimpleField>
    <SimpleField name="sap_materialdesc" type="string"></SimpleField>
    <SimpleField name="sap_serialnumber" type="int"></SimpleField>
    <SimpleField name="sap_materialnumber" type="string"></SimpleField>
    <SimpleField name="msid" type="int"></SimpleField>
</Schema>
<Folder><name>Download_Spasial_Kabel</name>
  <Placemark>
    <Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
    <ExtendedData><SchemaData >
        <SimpleData name="oid">54812580</SimpleData>
        <SimpleData name="system_id">CABL00000458488</SimpleData>
        <SimpleData name="cable_id">CABL_1DPR003_ODF01_DPRJ592_01</SimpleData>
        <SimpleData name="cable_name">ODF_1DPR003_ODF01-SPLC_DPRJ592</SimpleData>
        <SimpleData name="parent_id">OVH-Cable</SimpleData>
        <SimpleData name="parent_type">Pole</SimpleData>schemaUrl="#Download_Spasial_Kabel"
        <SimpleData name="region_name">RBNT</SimpleData>
        <SimpleData name="province_name">BALI</SimpleData>
        <SimpleData name="a_location">ODF_1DPR003_ODF01</SimpleData>
        <SimpleData name="b_location">SPLC_DPRJ592</SimpleData>
        <SimpleData name="total_core">12</SimpleData>
        <SimpleData name="no_of_tube">2</SimpleData>
        <SimpleData name="no_of_core_per_tube">6</SimpleData>
        <SimpleData name="cable_measured_length">2704.169</SimpleData>
        <SimpleData name="cable_calculated_length">2704.169</SimpleData>
        <SimpleData name="overheadunderground">Overhead</SimpleData>
        <SimpleData name="total_no_chamber_splice">0</SimpleData>
        <SimpleData name="coreaccess">Core</SimpleData>
        <SimpleData name="construction_stage">Built</SimpleData>
        <SimpleData name="activation_stage">Activated</SimpleData>
        <SimpleData name="accessibility">Yes</SimpleData>
        <SimpleData name="installation_year">2010</SimpleData>
        <SimpleData name="production_year">0</SimpleData>
        <SimpleData name="installation_company">PT. BANGUN PRIMA SEMESTA</SimpleData>
        <SimpleData name="installation_date">29/05/2015</SimpleData>
        <SimpleData name="user_remark">DATAENTRY_IMAM</SimpleData>
        <SimpleData name="status">A</SimpleData>
        <SimpleData name="user_id">80479</SimpleData>
        <SimpleData name="user_name">joko.siswanto</SimpleData>
        <SimpleData name="type">ADSS</SimpleData>
        <SimpleData name="r01_region_id">RBNT</SimpleData>
        <SimpleData name="r15_project_name">GI PESANGGARAN-GI NUSA DUA_DWDM</SimpleData>
        <SimpleData name="created_date">21/02/2018 12:23</SimpleData>
        <SimpleData name="sap_serialnumber">0</SimpleData>
        <SimpleData name="msid">17191</SimpleData>
    </SchemaData></ExtendedData>
      <LineString><coordinates>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</coordinates></LineString>
  </Placemark>
  </Folder>
  </Document>
</kml>
0

There are 0 answers