如何将此.csv文件转换为第一行中带有标签的.xml文件

时间:2016-02-19 03:38:11

标签: c# xml asp.net-mvc csv

这是我的.csv文件的一部分

CRUISE-ID;DEP-PORT;DEP-NAME-PORT;DEP-DATE;DEP-DAY;DEP-WEEKDAY;DEP-TIME;ARR-PORT;ARR-NAME-PORT;ARR-DATE;ARR-DAY;ARR-WEEKDAY;ARR-TIME;ITIN-CD;AREA/DEST
OR20160209BGIBGI;BGI;Bridgetown, Barbados;09/02/16;1;Tuesday;2000;POS;Port of Spain, Trinidad and Tobago;10/02/16;2;Wednesday;0900;U19M;CAR;
OR20160209BGIBGI;POS;Port of Spain, Trinidad and Tobago;10/02/16;2;Wednesday;1700;RSU;Roseau, Dominica;11/02/16;3;Thursday;1000;U19M;CAR;
OR20160209BGIBGI;RSU;Roseau, Dominica;11/02/16;3;Thursday;1800;STG;Saint George, Grenada;12/02/16;4;Friday;0800;U19M;CAR;
OR20160209BGIBGI;STG;Saint George, Grenada;12/02/16;4;Friday;1800;FDF;Fort de France, Martinique;13/02/16;5;Saturday;0800;U19M;CAR;

在第一行中,这些是xml的标签。我可以将这个标记转换为xml而不使用这些标记。

 var lines = File.ReadAllLines(csvpath);
            var xmlFile = new XElement("TopElement",
                lines.Select(line=> new XElement("Items",
                    line.Split(';')
                       .Select((column, index) => new XElement("Column" + index, column)))));
            xmlFile.Save(xmlpath);

然后输出就像这样

<TopElement>
  <Items>
    <Column0>CODE</Column0>
    <Column1>LOC-CD</Column1>
    <Column2>PORT-CD</Column2>
    <Column3>DESC</Column3>
    <Column4>START-DT</Column4>
    <Column5>END-DT</Column5>
    <Column6>SHIP-CD</Column6>
    <Column7>PAXTYPE</Column7>
    <Column8>INVENTORIED</Column8>
    <Column9>APPLY-TO</Column9>
    <Column10>REGION-CD</Column10>
    <Column11>PRICE-TYPE</Column11>
    <Column12>PRICE-BASIS</Column12>
    <Column13>PRICE-I</Column13>
    <Column14>PRICE-J</Column14>
    <Column15>PRICE-C</Column15>
    <Column16>PRICE-A</Column16>
    <Column17>PRICE-S</Column17>
    <Column18>DESC-LONG</Column18>
  </Items>
  <Items>
    <Column0>ACF01</Column0>
    <Column1>ARR</Column1>
    <Column2>ACE</Column2>
    <Column3>TOUR OF NORTHERN LANZAROTE</Column3>
    <Column4>24/12/15</Column4>
    <Column5>25/02/16</Column5>
    <Column6>OX</Column6>
    <Column7>*</Column7>
    <Column8>YES</Column8>
    <Column9>I,J,C,A,S</Column9>
    <Column10>WEE</Column10>
    <Column11>N/A</Column11>
    <Column12>P</Column12>
    <Column13>0</Column13>
    <Column14>32</Column14>

这意味着xml文件的元素也是值。我想要这样。

<row>
<CODE>ACF01</CODE>
<LOC-CD>ARR</LOC-CD>
<PORT-CD>ACE</PORT-CD>
<DESC>TOUR OF NORTHERN LANZAROTE</DESC>
<START-DT>24/12/15</START-DT>
<END-DT>25/02/16</END-DT>
<SHIP-CD>OX</SHIP-CD>
<PAXTYPE>*</PAXTYPE>
<INVENTORIED>YES</INVENTORIED>
<APPLY-TO>I,J,C,A,S</APPLY-TO>
<REGION-CD>WEE</REGION-CD>
<PRICE-TYPE>N/A</PRICE-TYPE>
<PRICE-BASIS>P</PRICE-BASIS>
<PRICE-I>0</PRICE-I>
<PRICE-J>32</PRICE-J>
<PRICE-C>32</PRICE-C>
<PRICE-A>45</PRICE-A>
<PRICE-S>45</PRICE-S>
<DESC-LONG> del Rio- have your camera ready for capturing enchanting views for your memory book.</DESC-LONG>
</row>

我该怎么做。请帮助我。

2 个答案:

答案 0 :(得分:1)

我假设您在名称的开头有标题行。

另外,我注意到其中一个列名称中有/,查询替换为_,因为xml无法接受

var lines = File.ReadAllLines(filename);
var columns =  lines[0].Split(';');

var xmlFile = new XElement("Rows",
    lines.Skip(1).Select(line => new XElement("Row",
        line.Split(';')
        .Where(s=> !string.IsNullOrEmpty(s))
           .Select((column, index) => new XElement(columns[index].Replace("/", "_"), column)))));

xmlFile.Save(ouputfile);

<强>输出继电器

<Rows>
  <Row>
    <CRUISE-ID>OR20160209BGIBGI</CRUISE-ID>
    <DEP-PORT>BGI</DEP-PORT>
    <DEP-NAME-PORT>Bridgetown, Barbados</DEP-NAME-PORT>
    <DEP-DATE>09/02/16</DEP-DATE>
    <DEP-DAY>1</DEP-DAY>
    <DEP-WEEKDAY>Tuesday</DEP-WEEKDAY>
    <DEP-TIME>2000</DEP-TIME>
    <ARR-PORT>POS</ARR-PORT>
    <ARR-NAME-PORT>Port of Spain, Trinidad and Tobago</ARR-NAME-PORT>
    <ARR-DATE>10/02/16</ARR-DATE>
    <ARR-DAY>2</ARR-DAY>
    <ARR-WEEKDAY>Wednesday</ARR-WEEKDAY>
    <ARR-TIME>0900</ARR-TIME>
    <ITIN-CD>U19M</ITIN-CD>
    <AREA_DEST>CAR</AREA_DEST>
  </Row>
  <Row>
    <CRUISE-ID>OR20160209BGIBGI</CRUISE-ID>
    <DEP-PORT>POS</DEP-PORT>
    <DEP-NAME-PORT>Port of Spain, Trinidad and Tobago</DEP-NAME-PORT>
    <DEP-DATE>10/02/16</DEP-DATE>
    <DEP-DAY>2</DEP-DAY>
    <DEP-WEEKDAY>Wednesday</DEP-WEEKDAY>
    <DEP-TIME>1700</DEP-TIME>
    <ARR-PORT>RSU</ARR-PORT>
    <ARR-NAME-PORT>Roseau, Dominica</ARR-NAME-PORT>
    <ARR-DATE>11/02/16</ARR-DATE>
    <ARR-DAY>3</ARR-DAY>
    <ARR-WEEKDAY>Thursday</ARR-WEEKDAY>
    <ARR-TIME>1000</ARR-TIME>
    <ITIN-CD>U19M</ITIN-CD>
    <AREA_DEST>CAR</AREA_DEST>
  </Row>
  <Row>
    <CRUISE-ID>OR20160209BGIBGI</CRUISE-ID>
    <DEP-PORT>RSU</DEP-PORT>
    <DEP-NAME-PORT>Roseau, Dominica</DEP-NAME-PORT>
    <DEP-DATE>11/02/16</DEP-DATE>
    <DEP-DAY>3</DEP-DAY>
    <DEP-WEEKDAY>Thursday</DEP-WEEKDAY>
    <DEP-TIME>1800</DEP-TIME>
    <ARR-PORT>STG</ARR-PORT>
    <ARR-NAME-PORT>Saint George, Grenada</ARR-NAME-PORT>
    <ARR-DATE>12/02/16</ARR-DATE>
    <ARR-DAY>4</ARR-DAY>
    <ARR-WEEKDAY>Friday</ARR-WEEKDAY>
    <ARR-TIME>0800</ARR-TIME>
    <ITIN-CD>U19M</ITIN-CD>
    <AREA_DEST>CAR</AREA_DEST>
  </Row>
  <Row>
    <CRUISE-ID>OR20160209BGIBGI</CRUISE-ID>
    <DEP-PORT>STG</DEP-PORT>
    <DEP-NAME-PORT>Saint George, Grenada</DEP-NAME-PORT>
    <DEP-DATE>12/02/16</DEP-DATE>
    <DEP-DAY>4</DEP-DAY>
    <DEP-WEEKDAY>Friday</DEP-WEEKDAY>
    <DEP-TIME>1800</DEP-TIME>
    <ARR-PORT>FDF</ARR-PORT>
    <ARR-NAME-PORT>Fort de France, Martinique</ARR-NAME-PORT>
    <ARR-DATE>13/02/16</ARR-DATE>
    <ARR-DAY>5</ARR-DAY>
    <ARR-WEEKDAY>Saturday</ARR-WEEKDAY>
    <ARR-TIME>0800</ARR-TIME>
    <ITIN-CD>U19M</ITIN-CD>
    <AREA_DEST>CAR</AREA_DEST>
  </Row>
</Rows>

答案 1 :(得分:0)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication11
{
    class Program
    {

        static void Main(string[] args)
        {
            string CSVData = "CRUISE-ID;DEP-PORT;DEP-NAME-PORT;DEP-DATE;DEP-DAY;DEP-WEEKDAY;DEP-TIME;ARR-PORT;ARR-NAME-PORT;ARR-DATE;ARR-DAY;ARR-WEEKDAY;ARR-TIME;ITIN-CD;AREA/DESTOR20160209BGIBGI;BGI;Bridgetown, Barbados;09/02/16;1;Tuesday;2000;POS;Port of Spain, Trinidad and Tobago;10/02/16;2;Wednesday;0900;U19M;CAR;OR20160209BGIBGI;POS;Port of Spain, Trinidad and Tobago;10/02/16;2;Wednesday;1700;RSU;Roseau, Dominica;11/02/16;3;Thursday;1000;U19M;CAR;OR20160209BGIBGI;RSU;Roseau, Dominica;11/02/16;3;Thursday;1800;STG;Saint George, Grenada;12/02/16;4;Friday;0800;U19M;CAR;OR20160209BGIBGI;STG;Saint George, Grenada;12/02/16;4;Friday;1800;FDF;Fort de France, Martinique;13/02/16;5;Saturday;0800;U19M;CAR;";
            List<string> ListData = CSVData.Split(';').ToList();
            int ElementNumber = int.Parse(Math.Ceiling(ListData.Count/19.0).ToString());
            StringBuilder XMLBuilder = new StringBuilder(5000);
            XMLBuilder.AppendFormat("<TopElement>");
            Console.WriteLine("<TopElement>");
            for (int n = 0; n < ElementNumber; n++)
            {
                int m = n;
                XMLBuilder.AppendFormat("<row>");
                Console.WriteLine("<row>");
                XMLBuilder.AppendFormat("<CODE>{0}</CODE>",m++<ListData.Count?ListData[m]:"");
                Console.WriteLine("<CODE>{0}</CODE>", m< ListData.Count ? ListData[m] : "" );
                XMLBuilder.AppendFormat("<LOC-CD>{0}</LOC-CD>", m++<ListData.Count?ListData[m]:"");
                Console.WriteLine("<LOC-CD>{0}</LOC-CD>", m < ListData.Count ? ListData[m] : "");
                XMLBuilder.AppendFormat("<PORT-CD>{0}</PORT-CD>", m++<ListData.Count?ListData[m]:"");
                Console.WriteLine("<PORT-CD>{0}</PORT-CD>", m < ListData.Count ? ListData[m] : "");
                XMLBuilder.AppendFormat("<DESC>{0}</DESC>", m++<ListData.Count?ListData[m]:"");
                Console.WriteLine("<DESC>{0}</DESC>", m < ListData.Count ? ListData[m] : "");
                XMLBuilder.AppendFormat("<START-DT>{0}</START-DT>", m++<ListData.Count?ListData[m]:"");
                Console.WriteLine("<START-DT>{0}</START-DT>", m < ListData.Count ? ListData[m] : "");
                XMLBuilder.AppendFormat("<END-DT>{0}</END-DT>", m++<ListData.Count?ListData[m]:"");
                Console.WriteLine("<END-DT>{0}</END-DT>", m < ListData.Count ? ListData[m] : "");
                XMLBuilder.AppendFormat("<SHIP-CD>{0}</SHIP-CD>", m++<ListData.Count?ListData[m]:"");
                Console.WriteLine("<SHIP-CD>{0}</SHIP-CD>", m < ListData.Count ? ListData[m] : "");
                XMLBuilder.AppendFormat("<PAXTYPE>{0}</PAXTYPE>", m++<ListData.Count?ListData[m]:"");
                Console.WriteLine("<PAXTYPE>{0}</PAXTYPE>", m < ListData.Count ? ListData[m] : "");
                XMLBuilder.AppendFormat("<INVENTORIED>{0}</INVENTORIED>", m++<ListData.Count?ListData[m]:"");
                Console.WriteLine("<INVENTORIED>{0}</INVENTORIED>", m < ListData.Count ? ListData[m] : "");
                XMLBuilder.AppendFormat("<APPLY-TO>{0}</APPLY-TO>", m++<ListData.Count?ListData[m]:"");
                Console.WriteLine("<APPLY-TO>{0}</APPLY-TO>", m < ListData.Count ? ListData[m] : "");
                XMLBuilder.AppendFormat("<REGION-CD>{0}</REGION-CD>", m++<ListData.Count?ListData[m]:"");
                Console.WriteLine("<REGION-CD>{0}</REGION-CD>", m < ListData.Count ? ListData[m] : "");
                XMLBuilder.AppendFormat("<PRICE-TYPE>{0}</PRICE-TYPE>", m++<ListData.Count?ListData[m]:"");
                Console.WriteLine("<PRICE-TYPE>{0}</PRICE-TYPE>", m < ListData.Count ? ListData[m] : "");
                XMLBuilder.AppendFormat("<PRICE-BASIS>{0}</PRICE-BASIS>", m++<ListData.Count?ListData[m]:"");
                Console.WriteLine("<PRICE-BASIS>{0}</PRICE-BASIS>", m < ListData.Count ? ListData[m] : "");
                XMLBuilder.AppendFormat("<PRICE-I>{0}</PRICE-I>", m++<ListData.Count?ListData[m]:"");
                Console.WriteLine("<PRICE-I>{0}</PRICE-I>", m < ListData.Count ? ListData[m] : "");
                XMLBuilder.AppendFormat("<PRICE-J>{0}</PRICE-J>", m++<ListData.Count?ListData[m]:"");
                Console.WriteLine("<PRICE-J>{0}</PRICE-J>", m < ListData.Count ? ListData[m] : "");
                XMLBuilder.AppendFormat("<PRICE-C>{0}</PRICE-C>", m++<ListData.Count?ListData[m]:"");
                Console.WriteLine("<PRICE-C>{0}</PRICE-C>", m < ListData.Count ? ListData[m] : "");
                XMLBuilder.AppendFormat("<PRICE-A>{0}</PRICE-A>", m++<ListData.Count?ListData[m]:"");
                Console.WriteLine("<PRICE-A>{0}</PRICE-A>", m < ListData.Count ? ListData[m] : "");
                XMLBuilder.AppendFormat("<PRICE-S>{0}</PRICE-S>", m++<ListData.Count?ListData[m]:"");
                Console.WriteLine("<PRICE-S>{0}</PRICE-S>", m < ListData.Count ? ListData[m] : "");
                XMLBuilder.AppendFormat("<DESC-LONG>{0}</DESC-LONG>", m++<ListData.Count?ListData[m]:"");
                Console.WriteLine("<DESC-LONG>{0}</DESC-LONG>", m < ListData.Count ? ListData[m] : "");
                XMLBuilder.AppendFormat("</row>");
                Console.WriteLine("</row>");
            }
            Console.WriteLine("</TopElement>");
            XMLBuilder.AppendFormat("</TopElement>");
            Console.ReadLine();
        }
    }
}

result