InvalidOperationException错误反映类

Jer*_*606 2 c# xml xml-serialization invalidoperationexception xmlserializer

读取多次与此错误相关的帖子并没有找到我的问题的解决方案后,我在这里解释.

我使用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; }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

Mik*_*ray 5

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

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

问题是你有一个可空的值类型作为property(TrendDatas.Id)被序列化为一个属性,XmlSerializer并没有很好地处理这些.此处此处列出了许多变通方法.他们都不是特别优雅.最佳选择可能是将Id元素的定义更改为:

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

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

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

    // ... snip ...
}
Run Code Online (Sandbox Code Playgroud)

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