質問

I am using XElement to create XML from a CSV in c#. I need to skip the first line and then process accordingly. I would like to skip the header of the csv so I am not publishing those values to the XML (THE FIELD[0] is dummy data for testing purposes. Here is my code snippet:

    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;
役に立ちましたか?

解決

Since you are reading all the contents at once using File.ReadAllLines you can do:

string[] csv = File.ReadAllLines(@"D:\PATH_TO_CSV_FILE.csv").Skip(1).ToArray();

You may see File.ReadLines, if you are going to read line by line.

EDIT: You can use File.ReadLines in your query like:

 XElement cust = new XElement("SUBMISSIONS",
            from str in File.ReadLines(@"D:\PATH_TO_CSV_FILE.csv").Skip(1)
            //..... rest of your query
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top