我正在使用XElement从c#中的CSV创建XML。我需要跳过第一行然后相应地处理。我想跳过csv的标题,所以我不会将这些值发布到XML(THE FIELD [0]是用于测试目的的虚拟数据。这是我的代码片段:
string[] csv = File.ReadAllLines(@"D:\PATH_TO_CSV_FILE.csv");
XElement cust = new XElement("SUBMISSIONS",
from str in csv
let fields = str.Split(',')
select new XElement("GSU_PROP_IN",
new XElement("FieldTypes",
new XElement(@"GSU_PROP_IN",
new XAttribute("class", "R"),
new XElement("BUSINESS_UNIT",
new XAttribute("type", "CHAR")),
new XElement("PROPOSAL_ID",
new XAttribute("type", "CHAR")),
new XElement("VERSION_ID", new XAttribute("type", "CHAR")),
new XElement("BEGIN_DT", new XAttribute("type", "DATE")),
new XElement("END_DT", new XAttribute("type", "DATE")),
new XElement("INSTID", new XAttribute("type", "CHAR")),
new XElement("MAJOR_SUBDIVISION", new XAttribute("type", "CHAR")),
new XElement("DEPTID", new XAttribute("type", "CHAR")),
new XElement("CUST_ID", new XAttribute("type", "CHAR")),
new XElement("BUSINESS_UNIT_AWD", new XAttribute("type", "CHAR")),
new XElement("DESCR20A", new XAttribute("type", "CHAR")),
new XElement("EMPLID", new XAttribute("type", "CHAR")),
new XElement("PURPOSE", new XAttribute("type", "CHAR")),
new XElement("TITLE56", new XAttribute("type", "CHAR")),
new XElement("DESCR254", new XAttribute("type", "CHAR")),
new XElement("BUILD_PERIODS", new XAttribute("type", "CHAR")),
new XElement("GSU_CONTACT", new XAttribute("type", "CHAR")),
new XElement("PRIMARY_FLAG", new XAttribute("type", "CHAR")),
new XElement("NO_OF_PERIODS", new XAttribute("type", "CHAR"))
),
new XElement(@"GSU_PROP_COST", new XAttribute("class", "R"),
new XElement("PROPOSAL_ID", new XAttribute("type", "CHAR")),
new XElement("VERSION_ID", new XAttribute("type", "CHAR")),
new XElement("BUDGET_PERIOD", new XAttribute("type", "CHAR")),
new XElement("GSU_DIRECT_COST", new XAttribute("type", "NUMBER")),
new XElement("GSU_INDIRECT_COST", new XAttribute("type", "NUMBER"))
),
new XElement(@"PSCAMA", new XAttribute("class", "R"),
new XElement("LANGUAGE_CD", new XAttribute("class", "CHAR")),
new XElement("AUDIT_ACTN", new XAttribute("class", "CHAR")),
new XElement("BASE_LANGUAGE_CD", new XAttribute("class", "CHAR")),
new XElement("MSG_SEQ_FLG", new XAttribute("class", "CHAR")),
new XElement("PROCESS_INSTANCE", new XAttribute("class", "NUMBER")),
new XElement("PUBLISH_RULE_ID", new XAttribute("class", "CHAR")),
new XElement("MSGNODENAME", new XAttribute("class", "CHAR"))
)
),
new XElement("MsgData",
new XElement("Transaction",
new XElement(@"GSU_PROP_IN", new XAttribute("class", "R"),
new XElement("BUSINESS_UNIT", new XAttribute("IsChanged", "Y"), fields[0]),
new XElement("PROPOSAL_ID", new XAttribute("IsChanged", "Y"), fields[0]),
new XElement("VERSION_ID", new XAttribute("IsChanged", "Y"), fields[0]),
new XElement("BEGIN_DT", new XAttribute("IsChanged", "Y"), fields[0]),
new XElement("END_DT", new XAttribute("IsChanged", "Y"), fields[0]),
new XElement("INSTID", new XAttribute("IsChanged", "Y"), fields[0]),
new XElement("MAJOR_SUBDIVISION", new XAttribute("IsChanged", "Y"), fields[0]),
new XElement("DEPTID", new XAttribute("IsChanged", "Y"), fields[0]),
new XElement("CUST_ID", new XAttribute("IsChanged", "Y"), fields[0]),
new XElement("BUSINESS_UNIT_AWD", new XAttribute("IsChanged", "Y"), fields[0]),
new XElement("DESCR20A", new XAttribute("IsChanged", "Y"), fields[0]),
new XElement("EMPLID", new XAttribute("IsChanged", "Y"), fields[0]),
new XElement("PURPOSE", new XAttribute("IsChanged", "Y"), fields[0]),
new XElement("TITLE56", new XAttribute("IsChanged", "Y"), fields[0]),
new XElement("DESCR254", new XAttribute("IsChanged", "Y"), fields[0]),
new XElement("BUILD_PERIODS", new XAttribute("IsChanged", "Y"), fields[0]),
new XElement("GSU_CONTACT", new XAttribute("IsChanged", "Y"), fields[0]),
new XElement("PRIMARY_FLAG", new XAttribute("IsChanged", "Y"), fields[0]),
new XElement("NO_OF_PERIODS", new XAttribute("IsChanged", "Y"), fields[0]),
new XElement(@"GSU_PROP_COST", new XAttribute("class", "R"),
new XElement("PROPOSAL_ID", new XAttribute("IsChanged", "Y"), fields[0]),
new XElement("VERSION_ID", new XAttribute("IsChanged", "Y"), fields[0]),
new XElement("BUDGET_PERIOD", new XAttribute("IsChanged", "Y"), fields[0]),
new XElement("GSU_DIRECT_COST", new XAttribute("IsChanged", "Y"), fields[0]),
new XElement("GSU_INDIRECT_COST", new XAttribute("IsChanged", "Y"), fields[0])
)
),
new XElement(@"PSCAMA", new XAttribute("class", "R"),
new XElement("LANGUAGE_CD", "ENG"),
new XElement("AUDIT_ACTN", new XAttribute("IsChanged", "Y"), "C"),
new XElement("BASE_LANGUAGE_CD", "ENG"),
new XElement("MSG_SEQ_FLG"),
new XElement("PROCESS_INSTANCE", "0"),
new XElement("PUBLISH_RULE_ID"),
new XElement("MSGNODENAME")
)
)
)
)
);
return cust;
答案 0 :(得分:0)
由于您使用File.ReadAllLines
一次阅读所有内容,您可以执行以下操作:
string[] csv = File.ReadAllLines(@"D:\PATH_TO_CSV_FILE.csv").Skip(1).ToArray();
如果您要逐行阅读,可能会看到File.ReadLines
。
编辑:您可以在查询中使用File.ReadLines
,例如:
XElement cust = new XElement("SUBMISSIONS",
from str in File.ReadLines(@"D:\PATH_TO_CSV_FILE.csv").Skip(1)
//..... rest of your query