Problème création fichier XML
Je souhaiterai ressortir le code CSV on XML, la première ligne étant le nom des colonnes. Ce code m'indique ceci: Code_postal
Exception non gérée*: System.InvalidOperationException: Le jeton StartAttribute
dans l'état Element Content générera un document XML non valide.
à System.Xml.XmlWellFormedWriter.ThrowInvalidStateTransition(Token token, Sta
te currentState)
à System.Xml.XmlWellFormedWriter.AdvanceState(Token token)
à System.Xml.XmlWellFormedWriter.WriteStartAttribute(String prefix, String lo
calName, String namespaceName)
à System.Xml.XmlWriter.WriteAttributeString(String localName, String value)
à ConsoleApplication2.Program.Main(String[] args) dans c:\Users\perso\Documen
ts\Visual Studio 2013\Projects\ConsoleApplication2\ConsoleApplication2\Program.c
s:ligne 63
Mon code :
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 73 74 75 76 77
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
List<string> lignes = new List<string>();
List<string> valeur = new List<string>();
List<string> entete = new List<string>();
string[] lines = System.IO.File.ReadAllLines(@"C:\cp.csv");
char delimiter = ';';
int count;
int nb = 0;
int nbligne = 0;
foreach (string line in lines)
{
lignes.Add(line);
String[] valeurs = line.Split(delimiter);
count = 0;
foreach (string value in valeurs)
{
//Console.WriteLine(value);
char[] charsToTrim = { '\"' };
string nvalue = value.Trim(charsToTrim);
if (nbligne == 0)
{
entete.Add(nvalue);
Console.WriteLine(nvalue);
}
else
{
valeur.Add(nvalue);
}
count ++;
}
nb = count;
nbligne++;
}
count = 0;
XmlWriter xmlWriter = XmlWriter.Create("test.xml");
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("lignes");
int monEntete = 0;
foreach (string unevaleur in valeur)
{
if (monEntete == 0)
{
xmlWriter.WriteStartElement("uneLigne");
}
xmlWriter.WriteAttributeString(entete[monEntete],unevaleur);
monEntete++;
if (monEntete == nb)
{
monEntete = 0;
}
xmlWriter.WriteEndElement();
}
xmlWriter.WriteEndDocument();
xmlWriter.Close();
}
}
} |