【问题标题】:to generate a xml file from csv ussing c# in ssis script component在 ssis 脚本组件中使用 c# 从 csv 生成 xml 文件
【发布时间】:2019-10-25 05:33:23
【问题描述】:

我有一个汽车保险记录的 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");

【问题讨论】:

  • 考虑尝试将 CSV 行读入可转换为 XElement 的对象 - 然后您可以拥有一个 Dictionary<string, List<Submission>>,如果有策略编号,您可以在其中添加到特定提交的列表中重复。
  • 添加了一个示例来帮助您入门 :-)。

标签: c# xml ssis script-component


【解决方案1】:

当您需要 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 将合并在一起。

这意味着在SubmissionID 发生冲突的情况下,只有PolicyDrivers 会发生变化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多