【问题标题】:Csv to XML, problem with the array stringCSV到XML,数组字符串有问题
【发布时间】:2021-12-02 20:31:50
【问题描述】:

大家好,我想将我的 CSV 文件(路径)转换为 XML 文件(保存路径)。 在 CSV 文件中有 13 个 headers 元素,我将所有元素放在一个数组字符串中。 我的问题是:我只想将数组的 1、2、3、10、11、12 元素放入 XML 文件中。 我该怎么做?跳过我只跳过第一个,但我如何跳过中间的元素? 非常感谢!!

        var lines = File.ReadAllLines(path);

        string[] headers;
        headers = new string[13] {"PlantNo", "No", "Name", "cod_fatt", "tags_grp", "dos_type", "pervuoti", "cemq_chk", "rapp_ac", "code_01", "unit_01", "peso_01",""};

        for (int i = 0; i < headers.Length; i ++)
        {
            lunghezza = i;
        }

        var xml = new XElement("DC8_Recipes",
           lines.Where((line, lunghezza) => lunghezza > 0).Select(line => new XElement("DC8_Recipes",
              line.Split(';').Skip(1).Select((column, lunghezza) => new XAttribute(headers[lunghezza], column)))));

        xml.Save(savepath);

【问题讨论】:

  • line.Split(';').Where((v, i) =&gt; !(i &lt; 1 || (i &gt; 4 &amp;&amp; i &lt; 10))) 应该为您提供索引 1-3 和 10-12 处的值
  • 是的,是函数!!

标签: c# arrays xml string csv


【解决方案1】:

您可以使用 LINQ Where() method with index enumeration 排除特定索引:

line.Split(';').Where((v, i) => !(i < 1 || (i > 4 && i < 10)))
// or 
var exclude = new int[] { 0, 5, 6, 7, 8, 9 };
line.Split(';').Where((v, i) => !exclude.Contains(i));
// or
var include = new int[] { 1, 2, 3, 4, 10, 11, 12 };
line.Split(';').Where((v, i) => include.Contains(i));

如果您有大量显式排除/包含,您可能希望将它们存储在搜索速度更快的数据类型中(例如HashSet&lt;int&gt;),但对于

【讨论】:

    【解决方案2】:

    我想,你需要这样的东西:

    using System.IO;
    using System.Xml.Linq;
    
    namespace CsvToXml
    {
        class Program
        {
            static void Main(string[] args)
            {
                const string csvFilePath = "C:\\Temp\\1.csv";
                const string xmlFilePath = "C:\\Temp\\1.xml";
    
                var indices = new int[] { 1, 2, 3, 10, 11, 12 };
                var headers = new string[] { "PlantNo", "No", "Name", "cod_fatt", "tags_grp", 
                                             "dos_type", "pervuoti", "cemq_chk", "rapp_ac", "code_01", 
                                             "unit_01", "peso_01", "not_empty_attribute_name" };
    
                var xItems = new XElement("items");
                var xDocument = new XDocument(xItems);
    
                var lines = File.ReadAllLines(csvFilePath);
                foreach (var line in lines)
                {
                    var xItem = new XElement("item");
                    var csvItems = line.Split(';');
                    foreach (var index in indices)
                        xItem.Add(new XAttribute(headers[index], csvItems[index]));
    
                    xItems.Add(xItem);
                }
    
                xDocument.Save(xmlFilePath);
            }
        }
    }
    

    输入文件“C:\Temp\1.csv”的例子是:

    0;+1;+2;+3;+4;+5;+6;+7;+8;+9;+10;+12;+13
    0;-1;-2;-3;-4;-5;-6;-7;-8;-9;-10;-12;+13
    

    输出文件“C:\Temp\1.xml”的例子是:

    <?xml version="1.0" encoding="utf-8"?>
    <items>
      <item No="+1" Name="+2" cod_fatt="+3" unit_01="+10" peso_01="+12" not_empty_attribute_name="+13" />
      <item No="-1" Name="-2" cod_fatt="-3" unit_01="-10" peso_01="-12" not_empty_attribute_name="+13" />
    </items>
    

    如果你想使用 linq:

    var indices = new int[] { 1, 2, 3, 10, 11, 12 };
    var xAttributes = indices.Select(x => new XAttribute(headers[x], csvItems[x]));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-19
      • 2011-03-19
      • 2022-10-18
      • 1970-01-01
      相关资源
      最近更新 更多