簡體   English   中英

在 Z2FAC6492C453AAF357EE45FA3537 腳本組件中使用 c# 從 csv 生成 xml 文件

[英]to generate a xml file from csv ussing c# in ssis script component

我有一個汽車保險記錄的 csv 文件數據,我需要使用 ssis 將其轉換為 xml 文件。 現在,當我在 csv 中有唯一的保險號碼時,我可以生成所需格式的 xml 文件,但保險號碼多次出現。 例如。 一個家庭已經為他們的汽車投保,有 4 名司機和 4 輛車,因此 csv 文件中有 4 個該保險號碼的條目,這應該是 xml 中的 1 個塊,其中保險號碼出現 1 次,所有駕駛員和車輛條目在 1 個保險號碼下 4 次標簽。

當沒有重復駕駛員或車輛或保險號碼時,我生成了一個 xml 文件。 我對 SSIS 非常陌生,並且從未在 c# 中完成編碼,所以如果有人可以幫助我編寫代碼。 如果列重復並為其創建子節點條目,如何循環它們。

這是我在 ssis 腳本組件中使用的代碼,用於生成我需要的 xml 的結構,但它僅在沒有重復的情況下才有效。

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");

當您需要 XML 文檔時,您可以將 CSV 讀取為對象並將構造的對象轉換為XElement

例子:

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");
    }
}

在示例中,我們創建了SubmissionDriver對象,其中一個提交可以有一個或多個PolicyDrivers 每個提交都存儲在字典中,其中鍵是SubmissionID - 因此,如果單個SubmissionID有多個條目,則PolicyDrivers將合並在一起。

這意味着只有PolicyDrivers會在SubmissionID發生沖突的情況下發生變化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM