<?xml version="1.0" encoding="utf-8"?> <Racine> <Balise1> <Balise2> <ListePersonnes> <Personne> <Nom Prenom="Alain">Dit</Nom> <DateNaisance>1990-06-06T00:00:00</DateNaisance> </Personne> <Personne> <Nom Prenom="Jean">Sort</Nom> <DateNaisance>2000-01-01T00:00:00</DateNaisance> </Personne> </ListePersonnes> </Balise2> </Balise1> </Racine>
private void tintin2256() { XDocument xdoc = XDocument.Load("test.xml"); XElement element = xdoc.Descendants("Personne").FirstOrDefault(e => e.Element("Nom").Value == "Dit"); if (element == null)//il n'y a pas d'occurence return; element.SetElementValue("DateNaisance", DateTime.Now); xdoc.Save("testModifie.xml"); }
En fait, le modèle "actuel" est assez basique et je compte le faire évoluer dans le futur d'où le choix de cette fonction.et le fait que dans ton code, tu itères 2 fois la collection ListeVannes m'interpelle.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <LDExchangeFile> <fileHeader company="Schneider Automation" product="Unity Pro XL V7.0 - 120823C" dateTime="date_and_time#2017-10-4-9:31:18" content="Fichier source LD" DTDVersion="41"></fileHeader> <contentHeader name="NVAAP_AP124" version="0.0.1" dateTime="date_and_time#2016-7-29-18:48:41"></contentHeader> <program> <identProgram name="ECH130011_Vannes_ASI" type="section" task="MAST"></identProgram> <LDSource nbColumns="11"> <networkLD> <typeLine> <emptyLine nbRows="1"></emptyLine> </typeLine> <typeLine> <contact typeContact="openContact" contactVariableName="false"></contact> <HLink nbCells="9"></HLink> <coil typeCoil="coil" coilVariableName="DFB_Valve_4.X"></coil> </typeLine> <typeLine> <emptyLine nbRows="1"></emptyLine> </typeLine> <typeLine> <emptyCell nbCells="4"></emptyCell> <FFBBlock instanceName="DFB_Valve_4" typeName="DFB_Valve" additionnalPinNumber="0" enEnO="true" width="12" height="6"> <objPosition posX="4" posY="3"></objPosition> <descriptionFFB execAfter=""> <inputVariable invertedPin="false" formalParameter="EN"></inputVariable> <inputVariable invertedPin="false" formalParameter="ZsOn" effectiveParameter="false"></inputVariable> <inputVariable invertedPin="false" formalParameter="ZsOff" effectiveParameter="false"></inputVariable> <inputVariable invertedPin="false" formalParameter="IN"></inputVariable> <inputVariable invertedPin="false" formalParameter="OUT"></inputVariable> <outputVariable invertedPin="false" formalParameter="ENO"></outputVariable> <outputVariable invertedPin="false" formalParameter="Xv"></outputVariable> </descriptionFFB> </FFBBlock> <emptyCell nbCells="5"></emptyCell> </typeLine> <typeLine> <HLink nbCells="4"></HLink> <emptyCell nbCells="7"></emptyCell> </typeLine> <typeLine> <emptyLine nbRows="6"></emptyLine> </typeLine> <typeLine> <contact typeContact="openContact" contactVariableName="false"></contact> <HLink nbCells="9"></HLink> <coil typeCoil="coil" coilVariableName="DFB_Valve_5.X"></coil> </typeLine> <typeLine> <emptyLine nbRows="1"></emptyLine> </typeLine> <typeLine> <emptyCell nbCells="4"></emptyCell> <FFBBlock instanceName="DFB_Valve_5" typeName="DFB_Valve" additionnalPinNumber="0" enEnO="true" width="12" height="6"> <objPosition posX="4" posY="13"></objPosition> <descriptionFFB execAfter=""> <inputVariable invertedPin="false" formalParameter="EN"></inputVariable> <inputVariable invertedPin="false" formalParameter="ZsOn" effectiveParameter="false"></inputVariable> <inputVariable invertedPin="false" formalParameter="ZsOff" effectiveParameter="false"></inputVariable> <inputVariable invertedPin="false" formalParameter="IN"></inputVariable> <inputVariable invertedPin="false" formalParameter="OUT"></inputVariable> <outputVariable invertedPin="false" formalParameter="ENO"></outputVariable> <outputVariable invertedPin="false" formalParameter="Xv"></outputVariable> </descriptionFFB> </FFBBlock> <emptyCell nbCells="5"></emptyCell> </typeLine> <typeLine> <HLink nbCells="4"></HLink> <emptyCell nbCells="7"></emptyCell> </typeLine> <typeLine> <emptyLine nbRows="83"></emptyLine> </typeLine> <textBox width="11" height="1">Gestion de la vanne de bypass Sortie Préchauffeur.<objPosition posX="0" posY="10"></objPosition> </textBox> <textBox width="11" height="1">Gestion de la vanne de bypass Sortie Préchauffeur.<objPosition posX="0" posY="0"></objPosition> </textBox> </networkLD> </LDSource> </program> <dataBlock> <variables name="DFB_Valve_5" typeName="DFB_Valve"></variables> <variables name="DFB_Valve_4" typeName="DFB_Valve"></variables> </dataBlock> </LDExchangeFile>
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question<typeLine> <emptyLine nbRows="1"></emptyLine> </typeLine> <typeLine> <contact typeContact="openContact" contactVariableName="false"></contact> <HLink nbCells="9"></HLink> <coil typeCoil="coil" coilVariableName="DFB_Valve_4.X"></coil> </typeLine> <typeLine> <emptyLine nbRows="1"></emptyLine> </typeLine>
<textBox width="11" height="1">Gestion de la vanne de bypass Sortie Préchauffeur.<objPosition posX="0" posY="10"></objPosition>
class LDExchangeFile { public LDExchangeFile() { Compagnie = "Schneider Automation"; Produit = "Unity Pro XL V7.0 - 120823C"; DateFileHeader = new DateTime(2017, 10, 4, 9, 31, 18); Contenu = "Fichier source LD"; DTDVersion = "41"; Nom = "NVAAP_AP124"; Version = "0.0.1"; DateContentHeader = new DateTime(2016, 7, 29, 18, 48, 41); Prog = new LeProgramme(); } public string Compagnie { get; set; } public string Produit { get; set; } public string Contenu { get; set; } public string DTDVersion { get; set; } public DateTime DateFileHeader { get; set; } public string Nom { get; set; } public string Version { get; set; } public DateTime DateContentHeader { get; set; } public LeProgramme Prog { get; set; } public void AddElementsLignes(params ElementLigne[] Elements) { Ligne ligne = new Ligne(); ligne.Elements = Elements.ToList(); Prog.LdSource.Elements.Add(ligne); } public void AddTextBox(int Y) { Prog.LdSource.Elements.Add(new TexteBox { Taille = new Size(0, Y) }); } public void Serialise(string Filename) { var toto = (from l in Prog.LdSource.Elements.OfType<Ligne>() from fb in l.Elements.OfType<FFBBlock>() select new { Nom = fb.NomInstance, Type = fb.NomType }); XDocument xdoc = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"), new XElement("LDExchangeFile", new XElement("fileHeader", new XAttribute("company", Compagnie), new XAttribute("product", Produit), new XAttribute("dateTime", string.Format("date_and_time#{0:yyyy-M-d-H:m:s}", DateFileHeader)), new XAttribute("content", Contenu), new XAttribute("DTDVersion", DTDVersion)), new XElement("contentHeader", new XAttribute("name", Nom), new XAttribute("version", Version), new XAttribute("dateTime", string.Format("date_and_time#{0:yyyy-M-d-H:m:s}", DateContentHeader))), Prog.Serialize, new XElement("dataBlock", from x in toto select new XElement("variables", new XAttribute("name", x.Nom), new XAttribute("typeName", x.Type)) ) ) ); string texte = xdoc.ToString().Replace(" ", "\t"); File.WriteAllText(Filename, Regex.Replace(texte, "<([a-z]+)( [^/]+)( />)", "<$1$2></$1>",RegexOptions.IgnoreCase)); } } class LeProgramme { public LeProgramme() { Nom = "ECH130011_Vannes_ASI"; Type = "section"; Tache = "MAST"; LdSource = new LDSource(); } public string Nom { get; set; } public string Type { get; set; } public string Tache { get; set; } public LDSource LdSource { get; set; } public XElement Serialize { get { return new XElement("program", new XElement("identProgram", new XAttribute("name", Nom), new XAttribute("type", Type), new XAttribute("task", Tache)), LdSource.Serialize ); } } } class LDSource { public LDSource() { NbreColonnes = 11; Elements = new List<NetworkLDElement>(); } public int NbreColonnes { get; set; } public List<NetworkLDElement> Elements { get; set; } public XElement Serialize { get { return new XElement("LDSource", new XAttribute("nbColumns", NbreColonnes), new XElement("networkLD", from e in Elements select e.Serialize )); } } } abstract class NetworkLDElement { public abstract XElement Serialize { get;} } class Ligne:NetworkLDElement { public List<ElementLigne> Elements { get; set; } public override XElement Serialize { get { return new XElement("typeLine", from l in Elements select l.Serialize); } } } abstract class ElementLigne { public abstract XElement Serialize { get; } } class LeContact:ElementLigne { public LeContact() { Type = "openContact"; Nom = "false"; } public string Type { get; set; } public string Nom { get; set; } public override XElement Serialize { get { return new XElement("contact", new XAttribute("typeContact", Type), new XAttribute("contactVariableName", Nom)); } } } class HLink:ElementLigne { public HLink() { NbreCells = 9; } public int NbreCells { get; set; } public override XElement Serialize { get { return new XElement("HLink", new XAttribute("nbCells", NbreCells)); } } } class Coil:ElementLigne { public Coil(string NomVariable) { Type = "coil"; this.NomVariable = NomVariable; } public string Type { get; set; }//si les types sont prédéfinis, on pourrait mettre un enum, ce serait valable plus haut aussi public string NomVariable { get; set; } public override XElement Serialize { get { return new XElement("coil", new XAttribute("typeCoil", Type), new XAttribute("coilVariableName",NomVariable)); } } } class EmptyLine:ElementLigne { public EmptyLine() { Nombre = 1; } public int Nombre { get; set; } public override XElement Serialize { get { return new XElement("emptyLine", new XAttribute("nbRows", Nombre)); } } } class EmptyCell:ElementLigne { public EmptyCell() { Nombre = 4; } public int Nombre { get; set; } public override XElement Serialize { get { return new XElement("emptyCell", new XAttribute("nbCells", Nombre)); } } } class FFBBlock:ElementLigne { public FFBBlock(string InstanceName) { NomInstance = InstanceName; NomType = "DFB_Valve"; AdditionnalPinNumber = 0; EnEnO = true; Taille = new Size(12, 6); ExecAfter = ""; Elements = new List<LaVariable>(); } public string NomInstance { get; set; } public string NomType { get; set; } public int AdditionnalPinNumber { get; set; } public bool EnEnO { get; set; } public Size Taille { get; set; } public Point Position { get; set; } public string ExecAfter { get; set; } public List<LaVariable> Elements { get; set; } public override XElement Serialize { get { return new XElement("FFBBlock", new XAttribute("instanceName", NomInstance), new XAttribute("typeName", NomType), new XAttribute("additionnalPinNumber", AdditionnalPinNumber), new XAttribute("enEnO", EnEnO), new XAttribute("width", Taille.Width), new XAttribute("height", Taille.Height), new XElement("objPosition", new XAttribute("posX", Position.X), new XAttribute("posY",Position.Y)), new XElement("descriptionFFB", new XAttribute("execAfter", ExecAfter), from e in Elements select e.Serialize ) ); } } } class LaVariable { public LaVariable(bool Input, string FormalParameter, bool? EffectiveParameter = null) { InvertedPin = false; this.FormalParameter = FormalParameter; this.EffectiveParameter = EffectiveParameter; input = Input; } bool input; public bool InvertedPin { get; set; } public string FormalParameter { get; set; } public bool? EffectiveParameter { get; set; } public XElement Serialize { get { return new XElement(input ? "inputVariable" : "outputVariable", new XAttribute("invertedPin", InvertedPin), new XAttribute("formalParameter", FormalParameter), EffectiveParameter == null ? null : new XAttribute("effectiveParameter", EffectiveParameter)); } } } class TexteBox:NetworkLDElement { public TexteBox() { Taille = new Size(11, 1); Texte = "Gestion de la vanne de bypass Sortie Préchauffeur."; Position = new Point(0, 0); } public Size Taille { get; set; } public string Texte { get; set; } public Point Position { get; set; } public override XElement Serialize { get { return new XElement("textBox", new XAttribute("width", Taille.Width), new XAttribute("height", Taille.Height), string.Format("{0}<objPosition posX=\"{1}\" posY=\"{2}\"",Texte,Position.X,Position.Y)) ; } } }
LDExchangeFile test = new LDExchangeFile(); test.AddElementsLignes(new EmptyLine()); test.AddElementsLignes(new LeContact(), new HLink(), new Coil("DFB_Valve_4.X")); test.AddElementsLignes(new EmptyLine()); FFBBlock block = new FFBBlock("DFB_Valve_4"); block.Position = new Point(4,3); block.Elements = new List<LaVariable> { new LaVariable(true,"EN"), new LaVariable(true,"ZsOn", false), new LaVariable(true,"ZsOff", false), new LaVariable(true, "IN"), new LaVariable(true, "OUT"), new LaVariable(false,"ENO"), new LaVariable(false,"Xv") }; test.AddElementsLignes(new EmptyCell { Nombre = 4 }, block , new EmptyCell { Nombre = 5 }); test.AddElementsLignes(new HLink { NbreCells = 4 }, new EmptyCell { Nombre = 7 }); test.AddElementsLignes(new EmptyLine { Nombre = 6 }); test.AddElementsLignes(new LeContact(), new HLink(), new Coil("DFB_Valve_5.X")); test.AddElementsLignes(new EmptyLine()); FFBBlock block2 = new FFBBlock("DFB_Valve_5"); block2.Position = new Point(4,13); block2.Elements = new List<LaVariable>(block.Elements); test.AddElementsLignes(new EmptyCell { Nombre = 4 }, block2 , new EmptyCell { Nombre = 5 }); test.AddElementsLignes(new HLink { NbreCells = 4 }, new EmptyCell { Nombre = 7 }); test.AddElementsLignes(new EmptyLine { Nombre = 83 }); test.AddTextBox(10); test.AddTextBox(0); test.Serialise("testTintin.xml");
<LDExchangeFile> <fileHeader company="Schneider Automation" product="Unity Pro XL V7.0 - 120823C" dateTime="date_and_time#2017-10-4-9:31:18" content="Fichier source LD" DTDVersion="41"></fileHeader> <contentHeader name="NVAAP_AP124" version="0.0.1" dateTime="date_and_time#2016-7-29-18:48:41"></contentHeader> <program> <identProgram name="ECH130011_Vannes_ASI" type="section" task="MAST"></identProgram> <LDSource nbColumns="11"> <networkLD> <typeLine> <emptyLine nbRows="1"></LDSource> </typeLine> <typeLine>
class LDExchangeFile { public LDExchangeFile() { Compagnie = "Schneider Automation"; Produit = "Unity Pro XL V7.0 - 120823C"; DateFileHeader = new DateTime(2017, 10, 4, 9, 31, 18); Contenu = "Fichier source LD"; DTDVersion = "41"; Nom = "NVAAP_AP124"; Version = "0.0.1"; DateContentHeader = new DateTime(2016, 7, 29, 18, 48, 41); Prog = new LeProgramme(); } public string Compagnie { get; set; } public string Produit { get; set; } public string Contenu { get; set; } public string DTDVersion { get; set; } public DateTime DateFileHeader { get; set; } public string Nom { get; set; } public string Version { get; set; } public DateTime DateContentHeader { get; set; } public LeProgramme Prog { get; set; } public void AddElementsLignes(params ElementLigne[] Elements) { Ligne ligne = new Ligne(); ligne.Elements = Elements.ToList(); Prog.LdSource.Elements.Add(ligne); } public void AddTextBox(int Y) { Prog.LdSource.Elements.Add(new TexteBox { Taille = new Size(0, Y) }); } public void Serialise(string Filename) { var toto = (from l in Prog.LdSource.Elements.OfType<Ligne>() from fb in l.Elements.OfType<FFBBlock>() select new { Nom = fb.NomInstance, Type = fb.NomType }); XDocument xdoc = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"), new XElement("LDExchangeFile", new XElement("fileHeader", new XAttribute("company", Compagnie), new XAttribute("product", Produit), new XAttribute("dateTime", string.Format("date_and_time#{0:yyyy-M-d-H:m:s}", DateFileHeader)), new XAttribute("content", Contenu), new XAttribute("DTDVersion", DTDVersion)), new XElement("contentHeader", new XAttribute("name", Nom), new XAttribute("version", Version), new XAttribute("dateTime", string.Format("date_and_time#{0:yyyy-M-d-H:m:s}", DateContentHeader))), Prog.Serialize, new XElement("dataBlock", from x in toto select new XElement("variables", new XAttribute("name", x.Nom), new XAttribute("typeName", x.Type)) ) ) ); string texte = xdoc.ToString().Replace(" ", "\t"); File.WriteAllText(Filename, texte); } } class LeProgramme { public LeProgramme() { Nom = "ECH130011_Vannes_ASI"; Type = "section"; Tache = "MAST"; LdSource = new LDSource(); } public string Nom { get; set; } public string Type { get; set; } public string Tache { get; set; } public LDSource LdSource { get; set; } public XElement Serialize { get { return new XElement("program", new XElement("identProgram", new XAttribute("name", Nom), new XAttribute("type", Type), new XAttribute("task", Tache)), LdSource.Serialize ); } } } class LDSource { public LDSource() { NbreColonnes = 11; Elements = new List<NetworkLDElement>(); } public int NbreColonnes { get; set; } public List<NetworkLDElement> Elements { get; set; } public XElement Serialize { get { return new XElement("LDSource", new XAttribute("nbColumns", NbreColonnes), new XElement("networkLD", from e in Elements select e.Serialize )); } } } abstract class NetworkLDElement { public abstract XElement Serialize { get; } } class Ligne : NetworkLDElement { public List<ElementLigne> Elements { get; set; } public override XElement Serialize { get { return new XElement("typeLine", from l in Elements select l.Serialize); } } } abstract class ElementLigne { public abstract XElement Serialize { get; } } class LeContact : ElementLigne { public LeContact() { Type = "openContact"; Nom = "false"; } public string Type { get; set; } public string Nom { get; set; } public override XElement Serialize { get { return new XElement("contact", new XAttribute("typeContact", Type), new XAttribute("contactVariableName", Nom)); } } } class HLink : ElementLigne { public HLink() { NbreCells = 9; } public int NbreCells { get; set; } public override XElement Serialize { get { return new XElement("HLink", new XAttribute("nbCells", NbreCells)); } } } class Coil : ElementLigne { public Coil(string NomVariable) { Type = "coil"; this.NomVariable = NomVariable; } public string Type { get; set; }//si les types sont prédéfinis, on pourrait mettre un enum, ce serait valable plus haut aussi public string NomVariable { get; set; } public override XElement Serialize { get { return new XElement("coil", new XAttribute("typeCoil", Type), new XAttribute("coilVariableName", NomVariable)); } } } class EmptyLine : ElementLigne { public EmptyLine() { Nombre = 1; } public int Nombre { get; set; } public override XElement Serialize { get { return new XElement("emptyLine", new XAttribute("nbRows", Nombre)); } } } class EmptyCell : ElementLigne { public EmptyCell() { Nombre = 4; } public int Nombre { get; set; } public override XElement Serialize { get { return new XElement("emptyCell", new XAttribute("nbCells", Nombre)); } } } class FFBBlock : ElementLigne { public FFBBlock(string InstanceName) { NomInstance = InstanceName; NomType = "DFB_Valve"; AdditionnalPinNumber = 0; EnEnO = true; Taille = new Size(12, 6); ExecAfter = ""; Elements = new List<LaVariable>(); } public string NomInstance { get; set; } public string NomType { get; set; } public int AdditionnalPinNumber { get; set; } public bool EnEnO { get; set; } public Size Taille { get; set; } public Point Position { get; set; } public string ExecAfter { get; set; } public List<LaVariable> Elements { get; set; } public override XElement Serialize { get { return new XElement("FFBBlock", new XAttribute("instanceName", NomInstance), new XAttribute("typeName", NomType), new XAttribute("additionnalPinNumber", AdditionnalPinNumber), new XAttribute("enEnO", EnEnO), new XAttribute("width", Taille.Width), new XAttribute("height", Taille.Height), new XElement("objPosition", new XAttribute("posX", Position.X), new XAttribute("posY", Position.Y)), new XElement("descriptionFFB", new XAttribute("execAfter", ExecAfter), from e in Elements select e.Serialize ) ); } } } class LaVariable { public LaVariable(bool Input, string FormalParameter, bool? EffectiveParameter = null) { InvertedPin = false; this.FormalParameter = FormalParameter; this.EffectiveParameter = EffectiveParameter; input = Input; } bool input; public bool InvertedPin { get; set; } public string FormalParameter { get; set; } public bool? EffectiveParameter { get; set; } public XElement Serialize { get { return new XElement(input ? "inputVariable" : "outputVariable", new XAttribute("invertedPin", InvertedPin), new XAttribute("formalParameter", FormalParameter), EffectiveParameter == null ? null : new XAttribute("effectiveParameter", EffectiveParameter)); } } } class TexteBox : NetworkLDElement { public TexteBox() { Taille = new Size(11, 1); Texte = "Gestion de la vanne de bypass Sortie Préchauffeur."; Position = new Point(0, 0); } public Size Taille { get; set; } public string Texte { get; set; } public Point Position { get; set; } public override XElement Serialize { get { return new XElement("textBox", new XAttribute("posY", Position.Y),new XAttribute("width", "11"), new XAttribute("height", "1"), Texte, new XElement("objPosition", new XAttribute("posX", Position.X), new XAttribute("posY", Position.Y))); } } } }
LDExchangeFile test = new LDExchangeFile(); test.AddElementsLignes(new EmptyLine()); test.AddElementsLignes(new LeContact(), new HLink(), new Coil("DFB_Valve_4.X")); test.AddElementsLignes(new EmptyLine()); FFBBlock block = new FFBBlock("DFB_Valve_4"); block.Position = new Point(4,3); block.Elements = new List<LaVariable> { new LaVariable(true,"EN"), new LaVariable(true,"ZsOn", false), new LaVariable(true,"ZsOff", false), new LaVariable(true, "IN"), new LaVariable(true, "OUT"), new LaVariable(false,"ENO"), new LaVariable(false,"Xv") }; test.AddElementsLignes(new EmptyCell { Nombre = 4 }, block , new EmptyCell { Nombre = 5 }); test.AddElementsLignes(new HLink { NbreCells = 4 }, new EmptyCell { Nombre = 7 }); test.AddElementsLignes(new EmptyLine { Nombre = 6 }); test.AddElementsLignes(new LeContact(), new HLink(), new Coil("DFB_Valve_5.X")); test.AddElementsLignes(new EmptyLine()); FFBBlock block2 = new FFBBlock("DFB_Valve_5"); block2.Position = new Point(4,13); block2.Elements = new List<LaVariable>(block.Elements); test.AddElementsLignes(new EmptyCell { Nombre = 4 }, block2 , new EmptyCell { Nombre = 5 }); test.AddElementsLignes(new HLink { NbreCells = 4 }, new EmptyCell { Nombre = 7 }); test.AddElementsLignes(new EmptyLine { Nombre = 83 }); test.AddTextBox(10); test.AddTextBox(0); test.Serialise(NomDossierDest.Text + @"\testTintin.xml"); }
class LDExchangeFile { public LDExchangeFile(string NomEquipement) { Prog = new LeProgramme(NomEquipement); } public LeProgramme Prog { get; set; } public void AddElementsLignes(params ElementLigne[] Elements) { Ligne ligne = new Ligne(); ligne.Elements = Elements.ToList(); Prog.LdSource.Elements.Add(ligne); } public void AddTextBox(string Com, int Y) { Prog.LdSource.Elements.Add(new TexteBox (Com, Y)); } public void Serialise(string Filename) { var toto = (from l in Prog.LdSource.Elements.OfType<Ligne>() from fb in l.Elements.OfType<FFBBlock_Valve>() select new { Nom = fb.NomVanne }); XDocument xdoc = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"), new XElement("LDExchangeFile", new XElement("fileHeader", new XAttribute("company", "Schneider Automation"), new XAttribute("product", "Unity Pro XL V7.0 - 120823C"), new XAttribute("dateTime", "date_and_time#2017-10-4-9:31:18")), new XAttribute("content", "Fichier source LD"), new XAttribute("DTDVersion", "41")), new XElement("contentHeader", new XAttribute("name", "NVAAP_AP124"), new XAttribute("version", "0.0.1"), new XAttribute("dateTime", "date_and_time#2016-7-29-18:48:41")), Prog.Serialize, new XElement("dataBlock", from x in toto select new XElement("variables", new XAttribute("name", x.Nom), new XAttribute("typeName", "DFB_Valve")) )); string texte = xdoc.ToString().Replace(" ", "\t"); File.WriteAllText(Filename, texte); } } class LeProgramme { public LeProgramme(string Nom) { this.Nom = Nom; LdSource = new LDSource(); } public string Nom { get; set; } public LDSource LdSource { get; set; } public XElement Serialize { get { return new XElement("program", new XElement("identProgram", new XAttribute("name", Nom), new XAttribute("type", "section"), new XAttribute("task", "MAST")), LdSource.Serialize ); } } } class LDSource { public LDSource() { Elements = new List<NetworkLDElement>(); } public List<NetworkLDElement> Elements { get; set; } public XElement Serialize { get { return new XElement("LDSource", new XAttribute("nbColumns", "11"), new XElement("networkLD", from e in Elements select e.Serialize )); } } } abstract class NetworkLDElement { public abstract XElement Serialize { get; } } class Ligne : NetworkLDElement { public List<ElementLigne> Elements { get; set; } public override XElement Serialize { get { return new XElement("typeLine", from l in Elements select l.Serialize); } } } abstract class ElementLigne { public abstract XElement Serialize { get; } } class LeContact : ElementLigne { public override XElement Serialize { get { return new XElement("contact", new XAttribute("typeContact", "openContact"), new XAttribute("contactVariableName", "false")); } } } class HLink : ElementLigne { public HLink(int NbreCells) { this.NbreCells = NbreCells; } public int NbreCells { get; set; } public override XElement Serialize { get { return new XElement("HLink", new XAttribute("nbCells", NbreCells)); } } } class Coil : ElementLigne { public Coil(string NomVariable) { this.NomVariable = NomVariable; } public string NomVariable { get; set; } public override XElement Serialize { get { return new XElement("coil", new XAttribute("typeCoil", "coil"), new XAttribute("coilVariableName", NomVariable)); } } } class EmptyLine : ElementLigne { public EmptyLine(int Nombre) { this.Nombre = Nombre; } public int Nombre { get; set; } public override XElement Serialize { get { return new XElement("emptyLine", new XAttribute("nbRows", Nombre)); } } } class EmptyCell : ElementLigne { public EmptyCell(int Nombre) { this.Nombre = Nombre; } public int Nombre { get; set; } public override XElement Serialize { get { return new XElement("emptyCell", new XAttribute("nbCells", Nombre)); } } } class FFBBlock_Valve : ElementLigne { public FFBBlock_Valve(string InstanceName, int PosY, string NomCaptON, string NomCaptOFF) { this.NomVanne = InstanceName; this.PosY = PosY; this.NomCaptON = NomCaptON; this.NomCaptOFF = NomCaptOFF; } public string NomVanne { get; set; } public int PosY { get; set; } public string NomCaptON { get; set; } public string NomCaptOFF { get; set; } public override XElement Serialize { get { return new XElement("FFBBlock", new XAttribute("instanceName", NomVanne), new XAttribute("typeName", "DFB_Valve"), new XAttribute("additionnalPinNumber", "0"), new XAttribute("enEnO", "true"), new XAttribute("width", "12"), new XAttribute("height", "6"), new XElement("objPosition", new XAttribute("posX", "4"), new XAttribute("posY", PosY)), new XElement("descriptionFFB", new XElement("inputVariable", new XAttribute("invertedPin", "false"), new XAttribute("formalParameter", "EN")), new XElement("inputVariable", new XAttribute("invertedPin", "false"), new XAttribute("formalParameter", "ZsOn"), new XAttribute("effectiveParameter", NomCaptON)), new XElement("inputVariable", new XAttribute("invertedPin", "false"), new XAttribute("formalParameter", "ZsOff"), new XAttribute("effectiveParameter", NomCaptOFF)), new XElement("inputVariable", new XAttribute("invertedPin", "false"), new XAttribute("formalParameter", "IN")), new XElement("inputVariable", new XAttribute("invertedPin", "false"), new XAttribute("formalParameter", "OUT")), new XElement("outputVariable", new XAttribute("invertedPin", "false"), new XAttribute("formalParameter", "ENO")), new XElement("outputVariable", new XAttribute("invertedPin", "false"), new XAttribute("formalParameter", "Xv"), new XAttribute("effectiveParameter", NomVanne)), new XAttribute("execAfter", "") ) ); } } } class TexteBox : NetworkLDElement { public TexteBox(String Texte, int posY) { this.Texte = Texte; this.posY = posY; } public Size Taille { get; set; } public string Texte { get; set; } public int posY { get; set; } public override XElement Serialize { get { return new XElement("textBox",new XAttribute("width", "11"), new XAttribute("height", "1"), Texte, new XElement("objPosition", new XAttribute("posX", 0), new XAttribute("posY", posY))); } } } }
LDExchangeFile test = new LDExchangeFile("Test"); test.AddElementsLignes(new EmptyLine(1)); test.AddElementsLignes(new LeContact(), new HLink(9), new Coil("DFB_Valve_4.X")); test.AddElementsLignes(new EmptyLine(1)); FFBBlock_Valve block = new FFBBlock_Valve("DFB_Valve_4",3,"test","test"); test.AddElementsLignes(new EmptyCell(4), new FFBBlock_Valve("DFB_Valve_4", 3, "test", "test"), new EmptyCell (5)); test.AddElementsLignes(new HLink(9), new EmptyCell(4)); test.AddElementsLignes(new EmptyLine (6)); test.AddElementsLignes(new LeContact(), new HLink(9), new Coil("DFB_Valve_5.X")); test.AddElementsLignes(new EmptyLine(1)); test.AddElementsLignes(new EmptyLine(83)); test.AddTextBox("Test_1",0); test.AddTextBox("Test_2", 10); test.Serialise(NomDossierDest.Text + @"\testTintin.xml");
XDocument xdoc = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"), new XElement("LDExchangeFile", new XElement("fileHeader", new XAttribute("company", "Schneider Automation"), new XAttribute("product", "Unity Pro XL V7.0 - 120823C"), new XAttribute("dateTime", "date_and_time#2017-10-4-9:31:18")), new XAttribute("content", "Fichier source LD"), new XAttribute("DTDVersion", "41")), new XElement("contentHeader", new XAttribute("name", "NVAAP_AP124"), new XAttribute("version", "0.0.1"), new XAttribute("dateTime", "date_and_time#2016-7-29-18:48:41")), Prog.Serialize, new XElement("dataBlock", from x in toto select new XElement("variables", new XAttribute("name", x.Nom), new XAttribute("typeName", "DFB_Valve")) ));
class LDExchangeFile { public LDExchangeFile(string NomEquipement) { Prog = new LeProgramme(NomEquipement); } public LeProgramme Prog { get; set; } public void AddElementsLignes(params ElementLigne[] Elements) { Ligne ligne = new Ligne(); ligne.Elements = Elements.ToList(); Prog.LdSource.Elements.Add(ligne); } public void AddTextBox(string Com, int Y) { Prog.LdSource.Elements.Add(new TexteBox (Com, Y)); } public void Serialise(string Filename) { var toto = (from l in Prog.LdSource.Elements.OfType<Ligne>() from fb in l.Elements.OfType<FFBBlock_Valve>() select new { Nom = fb.NomVanne }); XDocument xdoc = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"), new XElement("LDExchangeFile", new XElement("fileHeader", new XAttribute("company", "Schneider Automation"), new XAttribute("product", "Unity Pro XL V7.0 - 120823C"), new XAttribute("dateTime", "date_and_time#2017-10-4-9:31:18"), new XAttribute("content", "Fichier source LD"), new XAttribute("DTDVersion", "41")), new XElement("contentHeader", new XAttribute("name", "NVAAP_AP124"), new XAttribute("version", "0.0.1"), new XAttribute("dateTime", "date_and_time#2016-7-29-18:48:41")), Prog.Serialize, new XElement("dataBlock", from x in toto select new XElement("variables", new XAttribute("name", x.Nom), new XAttribute("typeName", "DFB_Valve")) ))); string texte = xdoc.ToString().Replace(" ", "\t"); File.WriteAllText(Filename, texte); } } class LeProgramme { public LeProgramme(string Nom) { this.Nom = Nom; LdSource = new LDSource(); } public string Nom { get; set; } public LDSource LdSource { get; set; } public XElement Serialize { get { return new XElement("program", new XElement("identProgram", new XAttribute("name", Nom), new XAttribute("type", "section"), new XAttribute("task", "MAST")), LdSource.Serialize ); } } } class LDSource { public LDSource() { Elements = new List<NetworkLDElement>(); } public List<NetworkLDElement> Elements { get; set; } public XElement Serialize { get { return new XElement("LDSource", new XAttribute("nbColumns", "11"), new XElement("networkLD", from e in Elements select e.Serialize )); } } } abstract class NetworkLDElement { public abstract XElement Serialize { get; } } class Ligne : NetworkLDElement { public List<ElementLigne> Elements { get; set; } public override XElement Serialize { get { return new XElement("typeLine", from l in Elements select l.Serialize); } } } abstract class ElementLigne { public abstract XElement Serialize { get; } } class LeContact : ElementLigne { public override XElement Serialize { get { return new XElement("contact", new XAttribute("typeContact", "openContact"), new XAttribute("contactVariableName", "false")); } } } class HLink : ElementLigne { public HLink(int NbreCells) { this.NbreCells = NbreCells; } public int NbreCells { get; set; } public override XElement Serialize { get { return new XElement("HLink", new XAttribute("nbCells", NbreCells)); } } } class Coil : ElementLigne { public Coil(string NomVariable) { this.NomVariable = NomVariable; } public string NomVariable { get; set; } public override XElement Serialize { get { return new XElement("coil", new XAttribute("typeCoil", "coil"), new XAttribute("coilVariableName", NomVariable)); } } } class EmptyLine : ElementLigne { public EmptyLine(int Nombre) { this.Nombre = Nombre; } public int Nombre { get; set; } public override XElement Serialize { get { return new XElement("emptyLine", new XAttribute("nbRows", Nombre)); } } } class EmptyCell : ElementLigne { public EmptyCell(int Nombre) { this.Nombre = Nombre; } public int Nombre { get; set; } public override XElement Serialize { get { return new XElement("emptyCell", new XAttribute("nbCells", Nombre)); } } } class FFBBlock_Valve : ElementLigne { public FFBBlock_Valve(string InstanceName, int PosY, string NomCaptON, string NomCaptOFF) { this.NomVanne = InstanceName; this.PosY = PosY; this.NomCaptON = NomCaptON; this.NomCaptOFF = NomCaptOFF; } public string NomVanne { get; set; } public int PosY { get; set; } public string NomCaptON { get; set; } public string NomCaptOFF { get; set; } public override XElement Serialize { get { return new XElement("FFBBlock", new XAttribute("instanceName", NomVanne), new XAttribute("typeName", "DFB_Valve"), new XAttribute("additionnalPinNumber", "0"), new XAttribute("enEnO", "true"), new XAttribute("width", "12"), new XAttribute("height", "6"), new XElement("objPosition", new XAttribute("posX", "4"), new XAttribute("posY", PosY)), new XElement("descriptionFFB", new XElement("inputVariable", new XAttribute("invertedPin", "false"), new XAttribute("formalParameter", "EN")), new XElement("inputVariable", new XAttribute("invertedPin", "false"), new XAttribute("formalParameter", "ZsOn"), new XAttribute("effectiveParameter", NomCaptON)), new XElement("inputVariable", new XAttribute("invertedPin", "false"), new XAttribute("formalParameter", "ZsOff"), new XAttribute("effectiveParameter", NomCaptOFF)), new XElement("inputVariable", new XAttribute("invertedPin", "false"), new XAttribute("formalParameter", "IN")), new XElement("inputVariable", new XAttribute("invertedPin", "false"), new XAttribute("formalParameter", "OUT")), new XElement("outputVariable", new XAttribute("invertedPin", "false"), new XAttribute("formalParameter", "ENO")), new XElement("outputVariable", new XAttribute("invertedPin", "false"), new XAttribute("formalParameter", "Xv"), new XAttribute("effectiveParameter", NomVanne)), new XAttribute("execAfter", "") ) ); } } } class TexteBox : NetworkLDElement { public TexteBox(String Texte, int posY) { this.Texte = Texte; this.posY = posY; } public Size Taille { get; set; } public string Texte { get; set; } public int posY { get; set; } public override XElement Serialize { get { return new XElement("textBox",new XAttribute("width", "11"), new XAttribute("height", "1"), Texte, new XElement("objPosition", new XAttribute("posX", 0), new XAttribute("posY", posY))); } } } }