Convertir CSV en XML de 6 niveaux
Bonjour,
Je suis débutant.
Je cherche à convertir un fichier csv en fichier XML.
J'ai réussi à faire que 2 niveaux et je trouve pas comment on peut convertir un CSV en XML de 4 niveaux . Mon code adapté (pris sur internet et adapté) est le suivant:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Xml;
namespace CSVToXML
{
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
/* string csvString = @"GREAL;Great Lakes Food Market;Howard Snyder;Marketing Manager;(503) 555-7555;2732 Baker Blvd.;Eugene;OR;97403;FR
HUNGC;Hungry Coyote Import Store;Yoshi Latimer;Sales Representative;(503) 555-6874;City Center Plaza 516 Main St.;Elgin;OR;97827;USA
LAZYK;Lazy K Kountry Store;John Steel;Marketing Manager;(509) 555-7969;12 Orchestra Terrace;Walla Walla;WA;99362;USA
LETSS;Let's Stop N Shop;Jaime Yorres;Owner;(415) 555-5938;87 Polk St. Suite 5;San Francisco;CA;94117;USA";*/
// File.WriteAllText("MonFichier.csv", csvString);
// Read into an array of strings.
string[] source = File.ReadAllLines("MonFichier.csv");
XElement cust = new XElement("Root",
from str in source
let fields = str.Split(';')
select new XElement("Customer",
new XAttribute("CustomerID", fields[0]),
new XElement("CompanyName", fields[1]),
new XElement("ContactName", fields[2]),
new XElement("ContactTitle", fields[3]),
/*new XElement("Phone", fields[4]),
new XElement("FullAddress",
new XElement("Address", fields[5]),
new XElement("City", fields[6]),
new XElement("Region", fields[7]),
new XElement("PostalCode", fields[8]),
new XElement("Country", fields[9])*/
)
)
// Je cherche ici à faire CustomerID comme niveau 2 de customer et companyName comme niveau 3 , contactName comme niveau 4
////
);
Console.WriteLine(cust);
richTextBox.Document.Blocks.Clear();
richTextBox.Document.Blocks.Add(new Paragraph(new Run(cust.ToString())));
cust.Save("fichier.xml");
}
}
} |
Pourriez-vous m'aider à faire 4 niveaux de balises xml ou lieux de 2 niveaux ?
Merci!