Salut à tous, pour le logiciel que créer en VB.Net, j'utilise une dll dans lequel on trouve le code suivant. Cette dll permet d'enregistrer dans un xml tous les contrôle d'une form, malheuresement, cette dll n'enregistre que les propriété Text, Location, BackColor et autre, elle ne fait pas toute les propriétés. Je ne sais pas faire de C#, j'ai tous de même essayé de modifier le code afin d'y rajouter la propriété Name. le code s'execute correctemment mais rien n'a changé dans mon fichier xml, je ne vois pas apparaitre la ligne à propos de Name. Il y a quelque chose qui cloche. Pouvez-vous m'aidez svp a résoudre le problême?
J'ai mis ci dessous le code modifier, je sais, j'en ais mit beaucoups mais je n'ai auccune idée de ce qui peut cloché, alors... (j'ai mis en rouge ce que j'ai modifié ou rajouté)
public class FormSerializer
{
private static void SerializeControls(XmlDocument doc, XmlNode parentNode, Control.ControlCollection controls)
{
foreach (Control control in controls)
{
XmlElement newElement = doc.CreateElement("Control");
XmlAttribute attribute = doc.CreateAttribute("ControlType");
attribute.Value = control.GetType().ToString();
newElement.Attributes.Append(attribute);
attribute = doc.CreateAttribute("Name");
attribute.Value = control.Name;
newElement.Attributes.Append(attribute); attribute = doc.CreateAttribute("Text");
attribute.Value = control.Text;
newElement.Attributes.Append(attribute);
attribute = doc.CreateAttribute("Top");
attribute.Value = control.Top.ToString();
newElement.Attributes.Append(attribute);
attribute = doc.CreateAttribute("Left");
attribute.Value = control.Left.ToString();
newElement.Attributes.Append(attribute);
attribute = doc.CreateAttribute("Width");
attribute.Value = control.Width.ToString();
newElement.Attributes.Append(attribute);
attribute = doc.CreateAttribute("Height");
attribute.Value = control.Height.ToString();
newElement.Attributes.Append(attribute);
attribute = doc.CreateAttribute("OnClick");
attribute.Value = "";
newElement.Attributes.Append(attribute);
parentNode.AppendChild(newElement);
if (control.HasChildren)
SerializeControls(doc, newElement, control.Controls);
}
}
public static XmlDocument Serialize(Form form)
{
XmlDocument doc = new XmlDocument();
XmlElement element = doc.CreateElement("root");
SerializeControls(doc, element, form.Controls);
doc.AppendChild(element);
return doc;
}
public static void Serialize(Form form, string fileName)
{
Serialize(form).Save(fileName);
}
public static void Deserialize(Form form, string fileName)
{
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
Deserialize(form, doc);
}
public static void Deserialize(Form form, XmlDocument doc)
{
if (!doc.HasChildNodes) return;
XmlNode root = doc.ChildNodes[0];
DeserializeControls(doc, root, form);
}
private static void DeserializeControls(XmlDocument doc, XmlNode parentNode, Control parentControl)
{
foreach (XmlNode node in parentNode.ChildNodes)
{
string typeName = node.Attributes["ControlType"].Value;
Assembly a = Assembly.GetAssembly(typeof(Form));
Type t = a.GetType(typeName);
Control c = (Control) Activator.CreateInstance(t);
c.Name = node.Attributes["Name"].Value; c.Text = node.Attributes["Text"].Value;
c.Top = Convert.ToInt32(node.Attributes["Top"].Value);
c.Left = Convert.ToInt32(node.Attributes["Left"].Value);
c.Width = Convert.ToInt32(node.Attributes["Width"].Value);
c.Height = Convert.ToInt32(node.Attributes["Height"].Value);
c.Parent = parentControl;
string clickMethod = node.Attributes["OnClick"].Value;
if (clickMethod != "")
{
Form f = parentControl.FindForm();
Type formType = f.GetType();
MethodInfo mi = formType.GetMethod(clickMethod);
if (mi != null)
c.Click += (EventHandler) Delegate.CreateDelegate(typeof(EventHandler), f, mi);
}
if (node.HasChildNodes)
DeserializeControls(doc, node, c);
}
}
et voici le code originale:
public class FormSerializer
{
private static void SerializeControls(XmlDocument doc, XmlNode parentNode, Control.ControlCollection controls)
{
foreach (Control control in controls)
{
XmlElement newElement = doc.CreateElement("Control");
XmlAttribute attribute = doc.CreateAttribute("ControlType");
attribute.Value = control.GetType().ToString();
newElement.Attributes.Append(attribute);
attribute = doc.CreateAttribute("Text");
attribute.Value = control.Text;
newElement.Attributes.Append(attribute);
attribute = doc.CreateAttribute("Top");
attribute.Value = control.Top.ToString();
newElement.Attributes.Append(attribute);
attribute = doc.CreateAttribute("Left");
attribute.Value = control.Left.ToString();
newElement.Attributes.Append(attribute);
attribute = doc.CreateAttribute("Width");
attribute.Value = control.Width.ToString();
newElement.Attributes.Append(attribute);
attribute = doc.CreateAttribute("Height");
attribute.Value = control.Height.ToString();
newElement.Attributes.Append(attribute);
attribute = doc.CreateAttribute("OnClick");
attribute.Value = "";
newElement.Attributes.Append(attribute);
parentNode.AppendChild(newElement);
if (control.HasChildren)
SerializeControls(doc, newElement, control.Controls);
}
}
public static XmlDocument Serialize(Form form)
{
XmlDocument doc = new XmlDocument();
XmlElement element = doc.CreateElement("root");
SerializeControls(doc, element, form.Controls);
doc.AppendChild(element);
return doc;
}
public static void Serialize(Form form, string fileName)
{
Serialize(form).Save(fileName);
}
public static void Deserialize(Form form, string fileName)
{
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
Deserialize(form, doc);
}
public static void Deserialize(Form form, XmlDocument doc)
{
if (!doc.HasChildNodes) return;
XmlNode root = doc.ChildNodes[0];
DeserializeControls(doc, root, form);
}
private static void DeserializeControls(XmlDocument doc, XmlNode parentNode, Control parentControl)
{
foreach (XmlNode node in parentNode.ChildNodes)
{
string typeName = node.Attributes["ControlType"].Value;
Assembly a = Assembly.GetAssembly(typeof(Form));
Type t = a.GetType(typeName);
Control c = (Control) Activator.CreateInstance(t);
c.Text = node.Attributes["Text"].Value;
c.Top = Convert.ToInt32(node.Attributes["Top"].Value);
c.Left = Convert.ToInt32(node.Attributes["Left"].Value);
c.Width = Convert.ToInt32(node.Attributes["Width"].Value);
c.Height = Convert.ToInt32(node.Attributes["Height"].Value);
c.Parent = parentControl;
string clickMethod = node.Attributes["OnClick"].Value;
if (clickMethod != "")
{
Form f = parentControl.FindForm();
Type formType = f.GetType();
MethodInfo mi = formType.GetMethod(clickMethod);
if (mi != null)
c.Click += (EventHandler) Delegate.CreateDelegate(typeof(EventHandler), f, mi);
}
if (node.HasChildNodes)
DeserializeControls(doc, node, c);
}
}


voila
Merci d'avance
a+
ebaudoux@club.fr
http://www.velersoftware.2007.fr
[ Lien ]