序列化图表和其他控件C#

时间:2013-03-18 21:40:48

标签: c# serialization charts xml-serialization

所以我正在尝试序列化我的图表,标签,文本框和组合框,我能够做所有事情,除了把它放在同一个xml文件中但图表状态,这就是我在序列化类中的内容

public static void Serialise(Control c, string XmlFileName, List<SampleFileObject> sampleObj) {
  XmlTextWriter xmlSerialisedForm = new XmlTextWriter(XmlFileName, System.Text.Encoding.Default);
  xmlSerialisedForm.Formatting = Formatting.Indented;
  xmlSerialisedForm.WriteStartDocument();
  xmlSerialisedForm.WriteStartElement("ChildForm");
  // enumerate all controls on the form, and serialise them as appropriate
  AddChildControls(xmlSerialisedForm, c);
  AddSampleList(xmlSerialisedForm, sampleObj);
  xmlSerialisedForm.WriteEndElement(); // ChildForm
  xmlSerialisedForm.WriteEndDocument();
  xmlSerialisedForm.Flush();
  xmlSerialisedForm.Close();
}

private static void AddChildControls(XmlTextWriter xmlSerialisedForm, Control c) {
  foreach (Control childCtrl in c.Controls) {
    if (!(childCtrl is Label)) {
      // serialise this control
      xmlSerialisedForm.WriteStartElement("Control");
      xmlSerialisedForm.WriteAttributeString("Type", childCtrl.GetType().ToString());
      xmlSerialisedForm.WriteAttributeString("Name", childCtrl.Name);
      if (childCtrl is TextBox) {
        xmlSerialisedForm.WriteElementString("Text", ((TextBox)childCtrl).Text);
      } else if (childCtrl is ComboBox) {
        xmlSerialisedForm.WriteElementString("Text", ((ComboBox)childCtrl).Text);
        xmlSerialisedForm.WriteElementString("SelectedIndex", ((ComboBox)childCtrl).SelectedIndex.ToString());
      } else if (childCtrl is ListBox) {
        // need to account for multiply selected items
        ListBox lst = (ListBox)childCtrl;
        if (lst.SelectedIndex == -1) {
          xmlSerialisedForm.WriteElementString("SelectedIndex", "-1");
        } else {
          for (int i = 0; i < lst.SelectedIndices.Count; i++) {
            xmlSerialisedForm.WriteElementString("SelectedIndex", (lst.SelectedIndices[i].ToString()));
          }
        }
      } else if (childCtrl is CheckBox) {
        xmlSerialisedForm.WriteElementString("Checked", ((CheckBox)childCtrl).Checked.ToString());
      }
      else if (childCtrl is Chart)
      {

        ((Chart)childCtrl).Serializer.Content = SerializationContents.Appearance;
        ((Chart)childCtrl).Serializer.SerializableContent += ",DataPoint.AxisLabel, Series.AxisLabels, Series.Name, ChartArea.Name";
        ((Chart)childCtrl).Serializer.Save(xmlSerialisedForm);
      }

我认为这是重要的部分。

由于

0 个答案:

没有答案