Пропуск первой строки csv при конвертации в XML в C#
Я использую XElement для создания XML из CSV в C#. Мне нужно пропустить первую строку и затем обработать соответственно. Я хотел бы пропустить заголовок 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;
1 ответ
Решение
Поскольку вы читаете все содержимое сразу, используя 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