Ajout de données dans fichier XML
Tintin2256 Messages postés 25 Date d'inscription Statut Membre Dernière intervention -
Bonjour à toute la communauté,
Je me permets de venir vers vous car j'ai une petite question sur la création d'un fichier XML en C#. Mes connaissances étant très limitées, je bloque vite...
Je veux générer ce code en sachant que le noeud "datablock" contiendra plusieurs "enfants" variables avec des attributs différents. J'ai pensé à utiliser la fonction XmlDocSource.Descendants mais je ne sais pas l'implémenter et je me dis qu'il y a peut être mieux que ça comme fonction. Ci après, le fichier exemple de sortie
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <STExchangeFile> <fileHeader company="Schneider Automation" product="Control Expert V14.1 - 191122A" dateTime="date_and_time#2022-11-12-9:21:30" content="Fichier source ST" DTDVersion="41"></fileHeader> <contentHeader name="Station" version="0.0.141" dateTime="date_and_time#2022-11-5-18:21:51"></contentHeader> <program> <identProgram name="copie_ES" type="section" task="MAST"></identProgram> <STSource>(*Copies des entrees ANA depuis le Wago *) EA_LT_Forage1 := Ilo_Wago_IN_AI_C1_V1 ; </STSource> </program> <dataBlock> <variables name="EA_LT_Forage1" typeName="INT"></variables> <variables name="EA_LT_Forage2" typeName="INT"></variables> </dataBlock> </STExchangeFile>
Voici le code utilisé:
// Création du fichier de sortie //Déclaration des différentes variables List<EnteteEANA> EnteteEAna = new List<EnteteEANA>(); List<EANA_API> Liste_EANA_API = new List<EANA_API>(); List<EnteteETOR> Entete_ETOR = new List<EnteteETOR>(); List<ETOR_API> Liste_ETOR_API = new List<ETOR_API>(); List<EnteteSTOR> Entete_STOR = new List<EnteteSTOR>(); List<STOR_API> Liste_STOR_API = new List<STOR_API>(); List<EnteteSANA> Entete_SANA = new List<EnteteSANA>(); List<SANA_API> Liste_SANA_API = new List<SANA_API>(); //Création des différentes entête de chaque partie EnteteEAna.Add(new EnteteEANA("(*Copies des entrees Analogiques *)")); Entete_SANA.Add(new EnteteSANA(" ")); Entete_SANA.Add(new EnteteSANA("(*Copies des sorties Analogiques *)")); Entete_ETOR.Add(new EnteteETOR(" ")); Entete_ETOR.Add(new EnteteETOR("(*Copies des entrees TOR *)")); Entete_STOR.Add(new EnteteSTOR(" ")); Entete_STOR.Add(new EnteteSTOR("(*Copies des sorties TOR *)")); int Index; //Ajout des différentes entrées ANA for (Index = 0; Index <= NB_EANA - 1; Index++) { Liste_EANA_API.Add(new EANA_API(Tab_EANA[Index, 1],Tab_EANA[Index, 0], Index)); } //Ajout des différentes entrées TOR for (Index = 0; Index <= NB_ETOR - 1; Index++) { Liste_ETOR_API.Add(new ETOR_API(Tab_ETOR[Index, 1], Tab_ETOR[Index, 0], Index)); } //Ajout des différentes sorties TOR for (Index = 0; Index <= NB_STOR - 1; Index++) { Liste_STOR_API.Add(new STOR_API(Tab_STOR[Index, 1], Tab_STOR[Index, 0], Index)); } //Ajout des différentes sorties ANA for (Index = 0; Index <= NB_SANA - 1; Index++) { Liste_SANA_API.Add(new SANA_API(Tab_SANA[Index, 1], Tab_SANA[Index, 0], Index)); } //Création du fichier XML source XDocument XmlDoc = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"), new XElement("STExchangeFile", new XElement("fileHeader", new XAttribute("company", "Schneider Automation"), new XAttribute("product", "Control Expert V14.1 - 191122A"), new XAttribute("dateTime", "date_and_time#2018-3-12-16:50:53"), new XAttribute("content", "Fichier source ST"), new XAttribute("DTDVersion", "41")), new XElement("contentHeader", new XAttribute("name", "Projet"), new XAttribute("version", "0.0.710"), new XAttribute("dateTime", "date_and_time#2018-2-14-14:39:27")), new XElement("program", new XElement("identProgram", new XAttribute("name", "Copie_E_S"), new XAttribute("type", "section"), new XAttribute("task", "MAST")), new XElement("STSource", from p in EnteteEAna select new XText(p.texte), from p in Liste_EANA_API select new XText(p.EANA), from p in Entete_ETOR select new XText(p.texte), from p in Liste_ETOR_API select new XText(p.ETOR), from p in Entete_STOR select new XText(p.texte), from p in Liste_STOR_API select new XText(p.STOR), from p in Entete_SANA select new XText(p.texte), from p in Liste_SANA_API select new XText(p.SANA))), new XElement("dataBlock", new XElement("variables"))));
//Création des différentes classes public class EnteteEANA { public EnteteEANA() { } public EnteteEANA(String texte) { this.texte = texte + "\n"; } public string texte { get; set; } } public class EANA_API { public EANA_API() { } public EANA_API(string Mnémonique, string Adresse, int Index) { this.EANA = "IF AOM_AO[" + Index + "] = 0 THEN " + Mnémonique + " := " + Adresse + "; ELSE " + Mnémonique + " := A_MODIFIER[" + Index + "]; END_IF;" + "\n"; } public string EANA { get; set; } public string Mnémonique { get; set; } public string Adresse { get; set; } public int Index { get; set; } } public class EnteteETOR { public EnteteETOR() { } public EnteteETOR(String texte) { this.texte = texte + "\n"; } public string texte { get; set; } } public class ETOR_API { public ETOR_API() { } public ETOR_API(string Mnémonique, string Adresse, int Index) { this.ETOR = Mnémonique + " := (" + Adresse + " AND AOM_I[" + Index + "] = 0) OR AOM_I[" + Index + "] = 1;" + "\n"; } public string ETOR { get; set; } public string Mnémonique { get; set; } public string Adresse { get; set; } public int Index { get; set; } } public class EnteteSTOR { public EnteteSTOR() { } public EnteteSTOR(String texte) { this.texte = texte + "\n"; } public string texte { get; set; } } public class STOR_API { public STOR_API() { } public STOR_API(string Mnémonique, string Adresse, int Index) { this.STOR = Adresse + " := (" + Mnémonique + " AND AOM_O[" + Index + "] = 0) OR AOM_O[" + Index + "] = 1;" + "\n"; } public string STOR { get; set; } public string Mnémonique { get; set; } public string Adresse { get; set; } public int Index { get; set; } } public class EnteteSANA { public EnteteSANA() { } public EnteteSANA(String texte) { this.texte = texte + "\n"; } public string texte { get; set; } } public class SANA_API { public SANA_API() { } public SANA_API(string Mnémonique, string Adresse, int Index) { this.SANA = "IF AOM_AO[" + Index + "] = 0 THEN " + Adresse + " := " + Mnémonique + "; ELSE " + Adresse + " := A_MODIFIER[" + Index + "]; END_IF;" + "\n"; } public string SANA { get; set; } public string Mnémonique { get; set; } public string Adresse { get; set; } public int Index { get; set; } }
En vous remerciant par avance de votre retour
Windows / Chrome 107.0.0.0
- Ajout de données dans fichier XML
- Fichier bin - Guide
- Fichier epub - Guide
- Fichier rar - Guide
- Comment réduire la taille d'un fichier - Guide
- Fichier .dat - Guide
21 réponses
Dans ma seconde fonction , je dois générer ceci:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <LDExchangeFile> <fileHeader company="Schneider Automation" product="Control Expert V14.1 - 191122A" dateTime="date_and_time#2022-11-17-14:38:39" content="Fichier source LD" DTDVersion="41"></fileHeader> <contentHeader name="Station" version="0.0.156" dateTime="date_and_time#2022-11-5-18:21:51"></contentHeader> <program> <identProgram name="Def_ANA_609_623" type="section" task="MAST"></identProgram> <LDSource nbColumns="11"> <networkLD> <typeLine> <emptyLine nbRows="2"></emptyLine> </typeLine> <typeLine> <contact typeContact="openContact" contactVariableName="TJRS_0"></contact> <HLink nbCells="9"></HLink> <coil typeCoil="setCoil" coilVariableName="DEF_609"></coil> </typeLine> <typeLine> <emptyLine nbRows="1"></emptyLine> </typeLine> <typeLine> <contact typeContact="openContact" contactVariableName="TJRS_0"></contact> <HLink nbCells="9"></HLink> <coil typeCoil="setCoil" coilVariableName="DEF_610"></coil> </typeLine> <typeLine> <emptyCell nbCells="4"></emptyCell> <FFBBlock instanceName="DEF_ANA_EANA_01" typeName="DEF_ANA" additionnalPinNumber="0" enEnO="false" width="16" height="8"> <objPosition posX="4" posY="5"></objPosition> <descriptionFFB execAfter=""> <inputVariable invertedPin="false" formalParameter="EN"></inputVariable> <inputVariable invertedPin="false" formalParameter="DEF_EANA"></inputVariable> <inputVariable invertedPin="false" formalParameter="BIT_SEC" effectiveParameter="BIT_1S"></inputVariable> <inputVariable invertedPin="false" formalParameter="MARCHE"></inputVariable> <inputVariable invertedPin="false" formalParameter="TAB_PARAM_DEFAUT" effectiveParameter="TAB_PARAM_DEF_EANA[1]"></inputVariable> <inputVariable invertedPin="false" formalParameter="MESURE_REEL" effectiveParameter="EANA_01"></inputVariable> <inputVariable invertedPin="false" formalParameter="FONCT_EN_DELTA_CONSIGNE" effectiveParameter="TJRS_0"></inputVariable> <outputVariable invertedPin="false" formalParameter="ENO"></outputVariable> <outputVariable invertedPin="false" formalParameter="DEFAUT_SEUIL_BAS"></outputVariable> <outputVariable invertedPin="false" formalParameter="ALARM_SEUIL_BAS"></outputVariable> <outputVariable invertedPin="false" formalParameter="ALARM_SEUIL_HAUT"></outputVariable> <outputVariable invertedPin="false" formalParameter="DEFAUT_SEUIL_HAUT"></outputVariable> </descriptionFFB> </FFBBlock> <emptyCell nbCells="5"></emptyCell> </typeLine> <typeLine> <emptyLine nbRows="1"></emptyLine> </typeLine> <typeLine> <shortCircuit> <VLink></VLink> <contact typeContact="openContact" contactVariableName="DEF_609"></contact> </shortCircuit> <HLink nbCells="3"></HLink> <emptyCell nbCells="2"></emptyCell> <HLink nbCells="4"></HLink> <coil typeCoil="setCoil" coilVariableName="DEF_611"></coil> </typeLine> <typeLine> <contact typeContact="openContact" contactVariableName="DEF_610"></contact> <emptyCell nbCells="5"></emptyCell> <HLink nbCells="4"></HLink> <coil typeCoil="coil" coilVariableName="DEF_612"></coil> </typeLine> <typeLine> <HLink nbCells="1"></HLink> <contact typeContact="openContact" contactVariableName="TJRS_0"></contact> <HLink nbCells="2"></HLink> <emptyCell nbCells="2"></emptyCell> <HLink nbCells="4"></HLink> <coil typeCoil="coil" coilVariableName="DEF_613"></coil> </typeLine> <typeLine> <emptyCell nbCells="6"></emptyCell> <HLink nbCells="4"></HLink> <coil typeCoil="setCoil" coilVariableName="DEF_614"></coil> </typeLine> <typeLine> <emptyLine nbRows="5"></emptyLine> </typeLine> <typeLine> <contact typeContact="openContact" contactVariableName="TJRS_0"></contact> <HLink nbCells="9"></HLink> <coil typeCoil="setCoil" coilVariableName="DEF_615"></coil> </typeLine> <typeLine> <emptyLine nbRows="2"></emptyLine> </typeLine> <typeLine> <contact typeContact="openContact" contactVariableName="TJRS_0"></contact> <HLink nbCells="9"></HLink> <coil typeCoil="setCoil" coilVariableName="DEF_616"></coil> </typeLine> <typeLine> <emptyCell nbCells="4"></emptyCell> <FFBBlock instanceName="DEF_ANA_EANA_02" typeName="DEF_ANA" additionnalPinNumber="0" enEnO="false" width="16" height="8"> <objPosition posX="4" posY="20"></objPosition> <descriptionFFB execAfter=""> <inputVariable invertedPin="false" formalParameter="EN"></inputVariable> <inputVariable invertedPin="false" formalParameter="DEF_EANA"></inputVariable> <inputVariable invertedPin="false" formalParameter="BIT_SEC" effectiveParameter="BIT_1S"></inputVariable> <inputVariable invertedPin="false" formalParameter="MARCHE"></inputVariable> <inputVariable invertedPin="false" formalParameter="TAB_PARAM_DEFAUT" effectiveParameter="TAB_PARAM_DEF_EANA[2]"></inputVariable> <inputVariable invertedPin="false" formalParameter="MESURE_REEL" effectiveParameter="EANA_02"></inputVariable> <inputVariable invertedPin="false" formalParameter="FONCT_EN_DELTA_CONSIGNE" effectiveParameter="TJRS_0"></inputVariable> <outputVariable invertedPin="false" formalParameter="ENO"></outputVariable> <outputVariable invertedPin="false" formalParameter="DEFAUT_SEUIL_BAS"></outputVariable> <outputVariable invertedPin="false" formalParameter="ALARM_SEUIL_BAS"></outputVariable> <outputVariable invertedPin="false" formalParameter="ALARM_SEUIL_HAUT"></outputVariable> <outputVariable invertedPin="false" formalParameter="DEFAUT_SEUIL_HAUT"></outputVariable> </descriptionFFB> </FFBBlock> <emptyCell nbCells="5"></emptyCell> </typeLine> <typeLine> <emptyLine nbRows="1"></emptyLine> </typeLine> <typeLine> <shortCircuit> <VLink></VLink> <contact typeContact="openContact" contactVariableName="DEF_615"></contact> </shortCircuit> <HLink nbCells="3"></HLink> <emptyCell nbCells="2"></emptyCell> <HLink nbCells="4"></HLink> <coil typeCoil="setCoil" coilVariableName="DEF_617"></coil> </typeLine> <typeLine> <contact typeContact="openContact" contactVariableName="DEF_616"></contact> <emptyCell nbCells="5"></emptyCell> <HLink nbCells="4"></HLink> <coil typeCoil="coil" coilVariableName="DEF_618"></coil> </typeLine> <typeLine> <HLink nbCells="1"></HLink> <contact typeContact="openContact" contactVariableName="TJRS_0"></contact> <HLink nbCells="2"></HLink> <emptyCell nbCells="2"></emptyCell> <HLink nbCells="4"></HLink> <coil typeCoil="coil" coilVariableName="DEF_619"></coil> </typeLine> <typeLine> <emptyCell nbCells="6"></emptyCell> <HLink nbCells="4"></HLink> <coil typeCoil="setCoil" coilVariableName="DEF_620"></coil> </typeLine> <typeLine> <emptyLine nbRows="3"></emptyLine> </typeLine> <textBox width="2" height="1">Défaut dépassement<objPosition posX="0" posY="18"></objPosition> </textBox> <textBox width="2" height="1">Défaut dépassement<objPosition posX="0" posY="3"></objPosition> </textBox> <textBox width="2" height="1">Défaut rupture fil<objPosition posX="0" posY="1"></objPosition> </textBox> <textBox width="2" height="1">Défaut rupture fil<objPosition posX="0" posY="15"></objPosition> </textBox> <textBox width="11" height="1">Gestion défauts analogiques EANA_02<objPosition posX="0" posY="14"></objPosition> </textBox> <textBox width="11" height="1">Gestion défauts analogiques EANA_01<objPosition posX="0" posY="0"></objPosition> </textBox> </networkLD> </LDSource> </program> <dataBlock> <variables name="EANA_01" typeName="REAL" topologicalAddress="%MW900"></variables> <variables name="EANA_02" typeName="REAL" topologicalAddress="%MW902"> </dataBlock> </LDExchangeFile>
Ma base excel est la même que celle que je t'ai donné la première fois à un détail près donc je te redonne un fichier à jour pour correspondre:
https://cjoint.com/c/LKrnRzsz5tI
J'ai commencé ma prog et j'arrive à générer les textbox et la partie "datablock" dans sa globalité mais je seche pour la génération du reste. Je dois absolument garder l'organisation comme elle est. On retrouve une architecture de base mais je ne parviens pas à la créer de manière "paramétrable"
<typeLine> <emptyLine nbRows="2"></emptyLine> </typeLine> <typeLine> <contact typeContact="openContact" contactVariableName="TJRS_0"></contact> <HLink nbCells="9"></HLink> <coil typeCoil="setCoil" coilVariableName="DEF_609"></coil> </typeLine> <typeLine> <emptyLine nbRows="1"></emptyLine> </typeLine> <typeLine> <contact typeContact="openContact" contactVariableName="TJRS_0"></contact> <HLink nbCells="9"></HLink> <coil typeCoil="setCoil" coilVariableName="DEF_610"></coil> </typeLine> <typeLine> <emptyCell nbCells="4"></emptyCell> <FFBBlock instanceName="DEF_ANA_EANA_01" typeName="DEF_ANA" additionnalPinNumber="0" enEnO="false" width="16" height="8"> <objPosition posX="4" posY="5"></objPosition> <descriptionFFB execAfter=""> <inputVariable invertedPin="false" formalParameter="EN"></inputVariable> <inputVariable invertedPin="false" formalParameter="DEF_EANA"></inputVariable> <inputVariable invertedPin="false" formalParameter="BIT_SEC" effectiveParameter="BIT_1S"></inputVariable> <inputVariable invertedPin="false" formalParameter="MARCHE"></inputVariable> <inputVariable invertedPin="false" formalParameter="TAB_PARAM_DEFAUT" effectiveParameter="TAB_PARAM_DEF_EANA[1]"></inputVariable> <inputVariable invertedPin="false" formalParameter="MESURE_REEL" effectiveParameter="EANA_01"></inputVariable> <inputVariable invertedPin="false" formalParameter="FONCT_EN_DELTA_CONSIGNE" effectiveParameter="TJRS_0"></inputVariable> <outputVariable invertedPin="false" formalParameter="ENO"></outputVariable> <outputVariable invertedPin="false" formalParameter="DEFAUT_SEUIL_BAS"></outputVariable> <outputVariable invertedPin="false" formalParameter="ALARM_SEUIL_BAS"></outputVariable> <outputVariable invertedPin="false" formalParameter="ALARM_SEUIL_HAUT"></outputVariable> <outputVariable invertedPin="false" formalParameter="DEFAUT_SEUIL_HAUT"></outputVariable> </descriptionFFB> </FFBBlock> <emptyCell nbCells="5"></emptyCell> </typeLine> <typeLine> <emptyLine nbRows="1"></emptyLine> </typeLine> <typeLine> <shortCircuit> <VLink></VLink> <contact typeContact="openContact" contactVariableName="DEF_609"></contact> </shortCircuit> <HLink nbCells="3"></HLink> <emptyCell nbCells="2"></emptyCell> <HLink nbCells="4"></HLink> <coil typeCoil="setCoil" coilVariableName="DEF_611"></coil> </typeLine> <typeLine> <contact typeContact="openContact" contactVariableName="DEF_610"></contact> <emptyCell nbCells="5"></emptyCell> <HLink nbCells="4"></HLink> <coil typeCoil="coil" coilVariableName="DEF_612"></coil> </typeLine> <typeLine> <HLink nbCells="1"></HLink> <contact typeContact="openContact" contactVariableName="TJRS_0"></contact> <HLink nbCells="2"></HLink> <emptyCell nbCells="2"></emptyCell> <HLink nbCells="4"></HLink> <coil typeCoil="coil" coilVariableName="DEF_613"></coil> </typeLine> <typeLine> <emptyCell nbCells="6"></emptyCell> <HLink nbCells="4"></HLink> <coil typeCoil="setCoil" coilVariableName="DEF_614"></coil> </typeLine>
Voici mon code:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////// //////////////////////////////////// //////////////////////////////////// Génération section de défauts //////////////////////////////////// //////////////////////////////////// //////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (SectionDefauts.Checked == true) { int Index_EANA = 0; //Constitution de la liste d'EntreeSortie List<EntreesANA> entreesANAS = new List<EntreesANA>(); for (int Index = 0; Index <= nb_lignesES - 1; Index++) { //Ajout des différentes entrées ANA if (Tableau_SourceES[Index, 1].Contains("AI_")) { entreesANAS.Add(new EANA(dataSetES.Tables[0].Rows[Index], Index_EANA)); Index_EANA = Index_EANA + 1; } } //Création du fichier XML source XDocument XmlDoc2 = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"), new XElement("LDExchangeFile", new XElement("fileHeader", new XAttribute("company", "Schneider Automation"), new XAttribute("product", "Control Expert V14.1 - 191122A"), new XAttribute("dateTime", "date_and_time#2018-3-12-16:50:53"), new XAttribute("content", "Fichier source ST"), new XAttribute("DTDVersion", "41")), new XElement("contentHeader", new XAttribute("name", "Station"), new XAttribute("version", "0.0.710"), new XAttribute("dateTime", "date_and_time#2018-2-14-14:39:27")), new XElement("program", new XElement("identProgram", new XAttribute("name", "Def_ANA"), new XAttribute("type", "section"), new XAttribute("task", "MAST")), new XElement("LDSource", new XAttribute("nbColumns", "11"), new XElement("networkLD", from es in entreesANAS select new XElement("textBox", new XAttribute("width", "11"), new XAttribute("height", "1"), new XElement("objPosition", new XAttribute("posX", "0"), new XAttribute("posY", 16 * es.Index), "Gestion défauts analogiques " + es.Mnemonique)), from es in entreesANAS select new XElement("textBox", new XAttribute("width", "2"), new XAttribute("height", "1"), new XElement("objPosition", new XAttribute("posX", "0"), new XAttribute("posY", 1 + 16 * es.Index), "Défaut rupture fil")), from es in entreesANAS select new XElement("textBox", new XAttribute("width", "2"), new XAttribute("height", "1"), new XElement("objPosition", new XAttribute("posX", "0"), new XAttribute("posY", 4 + 16 * es.Index), "Défaut dépassement")) ))), new XElement("dataBlock", from es in entreesANAS select new XElement("variables", new XAttribute("name", es.Nom_DFB), new XAttribute("typeName", "DEF_ANA"))) )); //Test si le fichier existe déjà if (File.Exists(NomDossierDest.Text + @"\Sections_LD.xld")) { MessageBox.Show("Un fichier section existe déjà dans le dossier de destination. Annulation de l'opération"); } else { //Récupération du nom de la zone pour définir le nom du fichier XmlDoc2.Save(NomDossierDest.Text + @"\Sections_LD.xld"); } } public abstract class EntreesANA { /// <summary> /// Le constructeur prend en paramètres un tableau de string qui correspond à une ligne du csv, et l'index /// </summary> /// <param name="LigneCSV"></param> /// <param name="Index"></param> public EntreesANA(DataRow LigneCSV, int Index) { this.Mnemonique = LigneCSV[9].ToString(); this.Adresse = LigneCSV[1].ToString(); this.Comment = $"{LigneCSV[5]} {LigneCSV[7]}"; this.Index = Index; this.Nom_DFB = $"DEF_ANA_{LigneCSV[9]}"; } public string Nom_DFB { get; set; } public string Mnemonique { get; set; } public string Adresse { get; set; } public int Index { get; set; } public string Comment { get; set; } } public class EANA : EntreesANA { //le constructeur appelle directement celui de la classe abstraite, et rien d'autre public EANA(DataRow LigneCSV, int Index) : base(LigneCSV, Index) { } //Overrider ToString est utile pour le débug, quand on regarde le contenu d'une liste, c'est ça qui est affiché public override string ToString() { return $"Index: {Index}, Adresse: {Adresse}, Mnémonique: {Mnemonique}"; } } }