Bonjour à tous,
je viens vous exposer mon problème en espérant trouver une solution car j'avoue être complètement coincé et être en manque de ressources..
J'ai développé une petite application qui me permet de convertir un fichier .csv en .xml, et maintenant je m'occupe de la conversion dans l'autre sens. Le gros soucis c'est que mon fichier xml est irrégulier, je suis donc coincé pour l'élaboration des colonnes du fichier .csv ainsi que leur remplissage.
Voici un exemple de mon fichier xml :
Comme vous pouvez le constater, parfois il y aura des Alarms, parfois non, et il pourra y en avoir jusqu'a 4 au maximum. Pour les balises property certaines ne seront pas toujours la.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <?xml version="1.0" encoding="utf-8" ?> <Tags> <Tag name="SO2EXTLATOUR12" path="" type="OPC"> <Property name="Datatype">6</Property> <Property name="Value">1</Property> <Property name="OPCServer">Ignition OPC-UA Server</Property> <Property name="OPCItemPath">[DEPOUSSIERAGE]DB50,X35.2</Property> </Tag> <Tag name="TESTNAME" path="" type="OPC"> <Property name="Datatype">6</Property> <Property name="Value">1</Property> <Property name="OPCServer">Ignition OPC-UA Server</Property> <Alarms> <Alarm name="Alarm 1"> <Property name="priority">1</Property> <Property name="setpointA">1.000000</Property> <Property name="displayPath">HISTO_CIFC</Property> </Alarm> </Alarms> </Tag> <Tag name="TESTNAME" path="" type="OPC"> <Property name="Datatype">6</Property> <Property name="Value">1</Property> <Property name="OPCServer">Ignition OPC-UA Server</Property> <Alarms> <Alarm name="Alarm 2"> <Property name="priority">1</Property> <Property name="setpointA">1.000000</Property> <Property name="displayPath">HISTO_CIFC</Property> </Alarm> <Alarm name="Alarm 3"> <Property name="priority">1</Property> <Property name="setpointA">1.000000</Property> <Property name="displayPath">HISTO_CIFC</Property> </Alarm> </Alarms> </Tag> </Tags>
Ainsi, après d'innombrables recherches, je suis tombé sur le site http://www.csvreader.com qui propose sont Framework mais je suis de nouveau coincé avec.. Pour ceux qui auraient eventuellement eu l'occasion de l'essayer un jour, voici mon code implementant la .dll en libre téléchargement :
Mais je vous rassure je n'ai pas plus de réussite..
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; using System.Xml.Linq; using LINQtoCSV; using DataStreams.Xml; using DataStreams.Csv; namespace ConvertisseurCsvToXml { public class XmlToCsv { [STAThread] public static void ConvertirToXml() { using (CsvWriter writer = new CsvWriter(@"C:\CSV-TO-XML\test.csv")) using (XmlRecordReader reader = new XmlRecordReader(@"C:\CSV-TO-XML\test.xml", "Tags/Tag", LoadMethod.InMemory)) { reader.Columns.Add("Tags/Tag/@name", "TagName"); reader.Columns.Add("Tags/Tag/@type", "TagType"); reader.Columns.Add("property", "DataType"); reader.Columns.Add("property", "Value"); reader.Columns.Add("property", "OPCServer"); reader.Columns.Add("property", "OPCItemPath"); reader.Columns.Add("Tags/Tag/Alarms/Alarm/@name", "AlarmName"); reader.Columns.Add("property", "priority"); reader.Columns.Add("property", "setpointA"); reader.Columns.Add("property", "displayPath"); writer.Write("TAG NAME"); writer.Write("TAG TYPE"); writer.Write("DATATYPE"); writer.Write("VALUE"); writer.Write("OPCSERVER"); writer.Write("OPCITEMPATH"); writer.Write("ALARM NAME"); writer.Write("PRIORITY"); writer.Write("SETPOINTA"); writer.Write("DISPLAYPATH"); writer.EndRecord(); while (reader.ReadRecord()) { writer.Write(reader["TagName"]); writer.Write(reader["TagType"]); writer.Write(reader["DataType"]); writer.Write(reader["Value"]); writer.Write(reader["OPCServer"]); writer.Write(reader["OPCItemPath"]); writer.Write(reader["AlarmName"]); writer.Write(reader["priority"]); writer.Write(reader["setpointA"]); writer.Write(reader["displayPath"]); writer.EndRecord(); } reader.Close(); writer.Close(); } } } }
Dans l'idée je veux un .csv de cette forme (à noter que les 2 premieres colonnes correspondent aux attributs du Tag, name="" et type="") alors que les autres correspondent à la valeur de la balise attribuée.
Si quelqu'un a une idée je suis disposé à échanger en vocal sur skype s'il le faut car je souhaite vraiment comprendre comment résoudre ce problème.
Cordialement,
B.
Partager