Home > front end >  Csv to XML, problem with the array string
Csv to XML, problem with the array string

Time:10-15

Hi all i want to convert my CSV file (path) in an XML file (savepath). In the CSV file there are 13 headers element and I put all in an array string. My problem is this: I want to put in XML file only the 1, 2, 3, 10, 11, 12 element of the array. How I can do it? With the skip I only skip the first but how i skip the element in the middle? Thank you a lot!!

        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);

CodePudding user response:

You could use the LINQ Where() method with index enumeration to exclude specific indices:

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));

If you have a large number of explicit exclusions/inclusions you might want to store them in a data type with faster search (eg. HashSet<int>), but for < 13 indices this should be fine.

CodePudding user response:

I think, you need something like this:

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);
        }
    }
}

The example of input file "C:\Temp\1.csv" is:

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

The example of output file "C:\Temp\1.xml" is:

<?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>

If you want to use linq:

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