InvalidOperationException错误反映类

时间:2013-12-08 22:11:18

标签: c# xml xml-serialization xmlserializer invalidoperationexception

在阅读了多次与此错误相关的帖子并且找不到我的问题的解决方案之后,我在这里解释一下。

我使用XmlSerializer来序列化简单类。

这是我的代码:

    private void btnGenerateXml_Click(object sender, RoutedEventArgs e)
    {
        Orchard orchard = new Orchard
        {
            Recipe = new Recipe
            {
                Name = "Generated by JooWeb.Tools",
                Author = "admin",
                ExportUtc = DateTime.UtcNow
            },
            MyDatas = new MyDatas
            {
                //Test = "test"
                TrendDatas = new TrendDatas
                {
                    Id = null,
                    Status = "Published",
                    TrendDatasPart = new TrendDatasPart
                    {
                        IdSource = 0,
                        PostalCode = "1000",
                        Locality = "Test5",
                        Surface = (decimal)0.00,
                        Price = (decimal)0.00,
                        Type = "",
                        InsertDateIndicator = "",
                        UpdateDateIndicator = "",
                        GetFromDate = DateTime.Now,
                        UpdatedDate = new DateTime(1900, 1, 1)
                    },
                    CommonPart = new CommonPart
                    {
                        Owner = "/User.UserName=admin",
                        CreatedUtc = DateTime.UtcNow,
                        PublishedUtc = DateTime.UtcNow,
                        ModifiedUtc = DateTime.UtcNow
                    }
                }
            }
        };

        XmlSerializer orchardXmlSerializer = new XmlSerializer(typeof(Orchard));
        var path = @"C:\Temp\orchardFileImport_" + string.Format("{0:yyyyMMdd}", DateTime.Today) + ".xml";
        if (File.Exists(path))
            File.Delete(path);
        orchardXmlSerializer.Serialize(File.OpenWrite(path), orchard);
        MessageBox.Show("Finished");
    }
}

[XmlRoot]
public class Orchard
{
    [XmlElement]
    public Recipe Recipe { get; set; }
    [XmlElement(ElementName = "Data")]
    public MyDatas MyDatas { get; set; }
}

public class Recipe
{
    [XmlElement]
    public string Name { get; set; }
    [XmlElement]
    public string Author { get; set; }
    [XmlElement]
    public DateTime ExportUtc { get; set; }
}

public class MyDatas
{
    public MyDatas()
    {

    }

    //[XmlElement]
    //public string Test { get; set; }

    [XmlElement]
    public TrendDatas TrendDatas { get; set; }
}

public class TrendDatas
{
    [XmlAttribute]
    public string Status { get; set; }
    [XmlAttribute]
    public int? Id { get; set; }
    //[XmlIgnore]
    [XmlElement]
    public TrendDatasPart TrendDatasPart { get; set; }
    //[XmlIgnore]
    [XmlElement]
    public CommonPart CommonPart { get; set; }
}

public class TrendDatasPart
{
    [XmlAttribute]
    public int IdSource { get; set; }
    [XmlAttribute]
    public string PostalCode { get; set; }
    [XmlAttribute]
    public string Locality { get; set; }
    [XmlAttribute]
    public decimal Surface { get; set; }
    [XmlAttribute]
    public decimal Price { get; set; }
    [XmlAttribute]
    public string Type { get; set; }
    [XmlAttribute]
    public string InsertDateIndicator { get; set; }
    [XmlAttribute]
    public string UpdateDateIndicator { get; set; }
    [XmlAttribute]
    public DateTime GetFromDate { get; set; }
    [XmlAttribute]
    public DateTime UpdatedDate { get; set; }
}

public class CommonPart
{
    [XmlAttribute]
    public string Owner { get; set; }
    [XmlAttribute]
    public DateTime CreatedUtc { get; set; }
    [XmlAttribute]
    public DateTime PublishedUtc { get; set; }
    [XmlAttribute]
    public DateTime ModifiedUtc { get; set; }
}

当我点击生成xml文件时使用此代码,我收到错误InvalidOperationException 有一个错误反映了'MergeExcelFiles.Orchard'类型。 {“有一个错误反映了'MyDatas'属性。”}

就像你在我的评论中看到的那样,我尝试将一个字符串xmlElement添加到节点MyDatas,这个更改我没有错误但是在xml文件中我没有任何名为Data的节点。

我不明白为什么类Recipe看起来都正确但是节点MyDatas在xml文件中没有显示或者出现此错误“InvalidOperationException”。

1 个答案:

答案 0 :(得分:5)

您需要更多地深入了解错误消息,因为原因在于最内层的异常:

  

System.InvalidOperationException:无法序列化System.Nullable`1 [System.Int32]类型的成员“Id”。 XmlAttribute / XmlText不能用于编码复杂类型。

问题在于您将可以为值的类型作为属性(TrendDatas.Id)作为属性进行序列化,而XmlSerializer不能很好地处理这些属性。列出了herehere的解决方法。他们都不是特别优雅。最佳选择可能是将Id的定义更改为元素:

public class TrendDatas
{
    // ... snip ...

    [XmlElement(IsNullable = true)]
    public int? Id { get; set; }

    public bool ShouldSerializeId() { return Id.HasValue; }

    // ... snip ...
}

ShouldSerializeId是一种方法,按照惯例,序列化程序用来决定是否应该在输出中序列化属性。在空值的情况下,序列化输出中不会定义任何元素。