0

I have a csv file data of automobile insurance records which i need to convert into a xml file using ssis. Now , when I have unique insurance numbers in csv then i can generate the xml file in the required format but there are multiple occurrences of a insurance number. For ex. A family have got their automobiles insured having 4 drivers and 4 vehicles so there are 4 entries of that insurance number in csv file which should be 1 block in xml having insurance number occurring 1 time and all driver and vehicle entries 4 time under 1 insurance number tag.

I have generated a xml file when there is no repetition of driver or vehicle or insurance number. I am very new to SSIS and never done coding in c# so if someone can help me with the code. How to loop over columns if they are repeating and make child nodes entry for it.

This is the code i have used in ssis script component to generate the structure of xml i require but it works only in case when there is no repitition.

string[] lines = File.ReadAllLines(@"H:\SSIS\Source\Intermediate.csv");

XElement xml = new XElement("Submissions",
    from str in lines
    let columns = str.Split(',')
    select new XElement("SubmissionEntry",
        new XElement("SubmissionID", columns[0]),
        new XElement("PolicyNumber", columns[1]),
        new XElement("OfferingCodeIdentifier", columns[2]),
        new XElement("BaseState", columns[3]),
        new XElement("EffectiveDate", columns[4]),
        new XElement("PeriodStart", columns[5]),
        new XElement("RateASOfDate", columns[6]),
        new XElement("RenewalNumber", columns[7]),
        new XElement("RatingCapFactor", columns[8]),
        new XElement("ConversionFactor", columns[9]),
        new XElement("ClaimsFreeCount", columns[10]),
        new XElement("PaidInFull", columns[11]),
        new XElement("IsHomeOwner", columns[12]),
        new XElement("IsNewBusinessTransfer", columns[13]),
        new XElement("IsNamedNonOwnerPolicy ", columns[14]),
        new XElement("LVTTier", columns[15]),
        new XElement("PNIBirthDate", columns[16]),
        new XElement("PNIPostalCode", columns[17]),
        new XElement("CreditStatus", columns[18]),
        new XElement("EquivalentCreditScore ", columns[19]),
        new XElement("CreditScore", columns[20]),
        new XElement("DeliverySource", columns[21]),
        new XElement("ChannelGroup", columns[22]),
        new XElement("LineCoverages",
            new XElement("LineCovEntry",
                new XElement("PatternCode", columns[23]),
                new XElement("CoverageTerms",
                    new XElement("CovTermCodeIdentifier", columns[24]),
                    new XElement("CovTermValue", columns[25])))),
        new XElement("PolicyDrivers",
            new XElement("DriverEntry",
                new XElement("DriverID", columns[26]),
                new XElement("DriverType", columns[27]),
                new XElement("Excluded", columns[28]),
                new XElement("RelationToApplicant", columns[29]),
                new XElement("DateOfBirth", columns[30]),
                new XElement("Gender", columns[31]),
                new XElement("MaritalStatus", columns[32]),
                new XElement("AgeLicensed", columns[33]),
                new XElement("LicenseStatus", columns[34]),
                new XElement("LicenseCountry", columns[35]),
                new XElement("UnverifiedDriver", columns[36]),
                new XElement("EmploymentStatus", columns[37]),
                new XElement("DriverImprovementCourse", columns[38]),
                new XElement("DriverImprovementCourse", columns[39]),
                    new XElement("IncidentEntry",
                        new XElement("IncidentID", columns[40]),
                        new XElement("IncidentDate", columns[41]),
                        new XElement("ViolationCode", columns[42]),
                        new XElement("OverrideCategory", columns[43]),
                        new XElement("LossAmount", columns[44])))),
        new XElement("PersonalVehicles",
            new XElement("VehicleEntry",
                new XElement("VehicleID", columns[45]),
                new XElement("VehicleYear", columns[46]),
                new XElement("GaragePostalCode", columns[47]),
                new XElement("PrimaryUse", columns[48]),
                new XElement("GaragedOutOfState3MonthsPerYear", columns[49]),
                new XElement("SecurityTypeCode", columns[50])),
                new XElement("RAPA",
                    new XElement("Rapa_Bi", columns[51]),
                    new XElement("Rapa_Coll", columns[52]),
                    new XElement("Rapa_Comp", columns[53]),
                    new XElement("Rapa_Med", columns[54]),
                    new XElement("Rapa_Pd", columns[55]),
                    new XElement("Rapa_Pip", columns[56])),
                    new XElement("VehicleCovEntry",
                        new XElement("PatternCode", columns[57]),
                        new XElement("CoverageTerm",
                            new XElement("CovTermCodeIdentifier", columns[58]),
                            new XElement("CovTermValue", columns[59]))))));

xml.Save(@"H:\SSIS\Destination\demo xml.xml");
AndreasHassing
  • 687
  • 4
  • 19
Urvi Jain
  • 1
  • 1
  • 1
  • Consider trying to read the CSV rows into objects that can be converted to `XElement`'s - then you can have a `Dictionary>` where you can add to the list of specific submissions if a policy number is repeated. – AndreasHassing Oct 25 '19 at 05:44
  • added an example to get you started :-). – AndreasHassing Oct 25 '19 at 06:03

1 Answers1

0

You can read the CSV into objects and convert the constructed objects into XElement when you need the XML document.

Example:

public class Example
{
    public class Driver
    {
        public string DriverId { get; set; }

        // .. add remaining properties

        public static Driver FromCsv(string[] row)
        {
            return new Driver
            {
                DriverId = row[26],
                // fill remaining driver properties with columns data
            };
        }

        public XElement ToXElement()
        {
            return new XElement("DriverEntry",
                new XElement("DriverID", DriverId)
                /* add remaining properties as XElement's */);
        }
    }

    public class Submission
    {
        public string SubmissionId { get; set; }

        public string PolicyNumber { get; set; }

        // .. add remaining properties

        public List<Driver> PolicyDrivers { get; set; }

        public static Submission FromCsv(string[] row)
        {
            return new Submission
            {
                SubmissionId = row[0],
                PolicyNumber = row[1],
                PolicyDrivers = new List<Driver> { Driver.FromCsv(row) },
                // fill remaining submission properties with columns data
            };
        }

        public XElement ToXElement()
        {
            return new XElement("SubmissionEntry",
                new XElement("SubmissionID", SubmissionId),
                new XElement("PolicyNumber", PolicyNumber),
                /* add remaining properties as XElement's */
                new XElement("PolicyDrivers",
                    PolicyDrivers.Select(d => d.ToXElement())));
        }
    }

    public static void ConvertToXml()
    {
        string[] lines = File.ReadAllLines(@"H:\SSIS\Source\Intermediate.csv");

        Dictionary<string, Submission> submissions = new Dictionary<string, Submission>();

        foreach (var line in lines)
        {
            var row = line.Split(',');

            var submissionId = row[0];

            if (submissions.ContainsKey(submissionId))
            {
                var submission = submissions[submissionId];

                submission.PolicyDrivers.Add(Driver.FromCsv(row));
            }
            else
            {

                submissions[submissionId] = Submission.FromCsv(row);
            }
        }

        XElement xml = new XElement("Submissions", submissions.Values.Select(s => s.ToXElement()));

        xml.Save(@"H:\SSIS\Destination\demo xml.xml");
    }
}

In the example we create Submission and Driver objects, where a submission can have one or more PolicyDrivers. Each submission is stored in a dictionary where the key is the SubmissionID - so if there are multiple entries for a single SubmissionID, the PolicyDrivers will get merged together.

This implies that only PolicyDrivers will change in the case of a collision on SubmissionID.

AndreasHassing
  • 687
  • 4
  • 19