【发布时间】: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