【问题标题】:There was an error generating the XML document生成 XML 文档时出错
【发布时间】:2011-07-03 07:39:18
【问题描述】:

您好,我有以下代码来执行 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);
            }



        }

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

我的表单保存类:

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>

【问题讨论】:

  • FormSaving 是您定义的自定义类吗?我以前没有听说过它,在谷歌上找不到任何关于它的参考。如果是,请您发布它,或者如果不是,请提供一个链接,我可以在其中找到更多信息。谢谢。
  • @joshhendo 嗨,我刚刚编辑了添加类。谢谢
  • 减号 1. 答案未标记为正确,在对多行代码进行排序后,很清楚发生了什么,但是对于这是一个很好的 SO 问题,答案应该清楚地表明洞察力进入问题并明确解决方案。整篇文章的最大问题是,点击此处链接的人很难自己解决这个问题。如果没有这些更改,这篇文章对 SO 知识库的贡献很小,而且比内容添加的噪音更多。
  • @MedicineMan 你在说什么?不太明白你
  • 第一:请将正确答案标记为正确。

标签: c# wpf xml-serialization


【解决方案1】:

StartzbuildcomboBox 的数据源是什么?

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

您还可以包含 InnerException 吗?

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

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

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

【讨论】:

  • 您好,谢谢您的意见。我没有任何真实的数据源。我在我的 xaml 中所做的是: library 我我也不确定数据项。什么是内部异常?
  • 快速修复:将 Startzbuildfrom 类型设置为 String 而不是 Object。并且在分配 abc.Startzbuildfrom = StartzbuildcomboBox.SelectedItem.ToString();这应该工作。
  • 加载时,组合框项仍未被选中:(
【解决方案2】:

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

【讨论】:

  • 嗨,保罗感谢您提供的信息,我不知道。所以如果我要序列化一个组合框选定的项目我应该怎么做呢?
  • object 是 C# 关键字,它是 Object 类型的别名,它是可序列化的。
  • 列表框的Value Member绑定的数据的实际类型是什么?您应该将它用于 Startzbuildfrom 属性的类型(只要它是可序列化的)。此外,使用 SelectedValue 而不是 SelectedItem。我已经更新了我的答案。抱歉,原件不完整且有错误。
  • @Vijay 是的,我原来的答案是错误的。对象是可序列化的。
【解决方案3】:

好的解决了,

我试过这个:

public class FormSaving
        {
            ...

            public int Startzbuildfrom
            {
                get;

                set;
            }


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

...
StartzbuildfromcomboBox.SelectedIndex = abc.Startzbuildfrom;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多