生成XML文档时出错

时间:2011-02-24 05:46:45

标签: c# wpf xml-serialization

我有以下代码来执行xml序列化:

private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            string savepath;
            SaveFileDialog DialogSave = new SaveFileDialog();
            // Default file extension
            DialogSave.DefaultExt = "txt";
            // Available file extensions
            DialogSave.Filter = "XML file (*.xml)|*.xml|All files (*.*)|*.*";
            // Adds a extension if the user does not
            DialogSave.AddExtension = true;
            // Restores the selected directory, next time
            DialogSave.RestoreDirectory = true;
            // Dialog title
            DialogSave.Title = "Where do you want to save the file?";
            // Startup directory
            DialogSave.InitialDirectory = @"C:/";
            DialogSave.ShowDialog();
            savepath = DialogSave.FileName;
            DialogSave.Dispose();
            DialogSave = null;

            FormSaving abc = new FormSaving();
            if (MajorversionresultLabel != null && MajorversionresultLabel.Content != null && MajorversionLabel.Content.ToString() != string.Empty)
            abc.Majorversion = MajorversionresultLabel.Content.ToString();
            //abc.Minorversion = MinorversionresultLabel.Content.ToString();
            //abc.Projectnumber = ProjectnumberresultLabel.Content.ToString();
            //abc.Buildnumber = BuildnumberresultLabel.Content.ToString();
            //abc.Previousbuildversion = PreviousbuildversionresultLabel.Content.ToString();
            abc.Startzbuildfrom = StartzbuildfromcomboBox.SelectedItem;

            using (Stream savestream = new FileStream(savepath, FileMode.Create))
            {

                    XmlSerializer serializer = new XmlSerializer(typeof(FormSaving));
                    serializer.Serialize(savestream, abc);
            }



        }

错误“生成XML文档时出错”发生在serializer.Serialize(savestream, abc);

我的表单保存类:

public class FormSaving
        {

            public string Majorversion
            {
                get;

                set;

            }
            public string Minorversion
            {
                get;

                set;

            }
            public string Projectnumber
            {
                get;

                set;

            }
            public string Buildnumber
            {
                get;

                set;

            }
            public string Previousbuildversion
            {
                get;

                set;

            }
            public object Startzbuildfrom
            {
                get;

                set;
            }
    }

任何人都可以帮我解决这个问题吗?

编辑:

我试过这个,但它也不起作用:

在“保存按钮”下

abc.Startzbuildfrom = StartzbuildfromcomboBox.SelectedItem.ToString();

在“加载按钮”下

StartzbuildfromcomboBox.SelectedItem = abc.Startzbuildfrom;

这是我如何填充我的组合框项目:

<ComboBox Height="23" Margin="577,72,497,0" Name="StartzbuildfromcomboBox" VerticalAlignment="Top"><ComboBoxItem>library</ComboBoxItem></ComboBox>

3 个答案:

答案 0 :(得分:0)

虽然object在技术上是一个可序列化类型,但Startzbuildfrom的具体类型是模糊的。实际上,您正在尝试序列化一个不可序列化的ComboBoxItem。尝试对Startzbuildfrom属性使用可序列化类型,并使用组合框的SelectedValue属性设置其值,而不是SelectedItem属性。

答案 1 :(得分:0)

StartzbuildcomboBox的数据源是什么?

更具体地说,每个StartzbuildcomboBox.SelectedItem的DataItem类型是什么?

你还可以包含InnerException吗?

最可能的原因可能是Startzbuildfrom(不应该是StartzBuildFrom?)被分配给XmlSerializer不知道的类型。

如果您知道该类型,则使用XmlInclude修饰FormSaving。

[XmlInclude(typeof(type-of-selected-combobox-selected-item))]
public class FormSaving
{
  .........

答案 2 :(得分:0)

好的解决了,

我试过这个:

public class FormSaving
        {
            ...

            public int Startzbuildfrom
            {
                get;

                set;
            }


        }
...
abc.Startzbuildfrom = StartzbuildfromcomboBox.SelectedIndex;

...
StartzbuildfromcomboBox.SelectedIndex = abc.Startzbuildfrom;