c# - 反序列化数组总是为元素返回 null 但适用于属性

标签 c# .net arrays serialization deserialization

反序列化部分工作时遇到问题。当我有一个带有属性的 xml 节点时,所有属性值都会正确加载到我的类中,但是当我使用元素时,它只返回 null。

我将以下内容存储在一个 xml 文件中:

<?xml version="1.0" encoding="utf-8"?>
<email>
  <from>admin@company.com</from>
  <recipients>
    <recipient>user1@company.com</recipient>  
    <recipient>user2@company.com</recipient>
  </recipients>
  <subject>Test subject</subject>
  <body>Test body</body>
  <attachments>
    <attachment>c:\test.txt</attachment>
    <attachment>c:\test1.txt</attachment>
    <attachment>c:\test2.txt</attachment>
  </attachments>
</email>

我的主类定义如下:

[Serializable]
[XmlRoot("email")]
public class EmailNotification
{
    [XmlElement("from")]
    public string From { get; set; }

    [XmlElement("subject")]
    public string Subject { get; set; }

    [XmlElement("body")]
    public string Body { get; set; }

    [XmlArray("recipients")]
    [XmlArrayItem("recipient")]
    public List<EmailNotificationRecipient> Recipients { get; set; }

    [XmlArray("attachments")]
    [XmlArrayItem("attachment")]
    public List<EmailNotificationAttachment> Attachments { get; set; }

    public EmailNotification()
    {
        this.Attachments = new List<EmailNotificationAttachment>();
        this.Recipients = new List<EmailNotificationRecipient>();
    }
}

这是我的收件人类别:

[Serializable]
public class EmailNotificationRecipient
{
    public EmailNotificationRecipient()
    {

    }

    [XmlElement("recipient")]
    public string Recipient { get; set; }
}

我不会费心显示附件类,因为它在作为收件人类的定义方面是相同的。

所有简单元素都被正确填充,我的数组正在构建,但其中的所有元素都是空的。

调试时,我可以看到我的数组有 2 个收件人项目和 3 个附件项目,但是当我检查里面的值时,每个数组项目都是空的。

我还在 EmailNotificationRecipient 类中添加了一个构造函数并在其上设置了一个断点,它每次都会为每个定义的收件人设置我的断点。

有了以上两点,它会让你相信我的类定义一切正常,或者它无法找到正确数量的元素,但如前所述,我的数组中的所有对象都设置为空,即使创建了正确数量的对象。

最初我用类型定义了 XmlArrayItem:

    [XmlArrayItem("recipient", typeof(EmailNotificationRecipient))]
    public List<EmailNotificationRecipient> Recipients { get; set; }

我删除了它,看看它是否会有所不同,但无济于事。

我错过了什么?这让我疯狂!!!

谢谢。

更新

请参阅下面@NoIdeaForName 的回答。

我目前定义它的方式意味着为了让它返回一个值,我的 xml 应该看起来像:

<recipients>
  <recipient><recipient></recipient></recipient>
</recipients>

因为我的数组需要一个类接收者,而事实上我在每个接收者节点中只有一个字符串,所以数组应该定义如下:

    [XmlArray("recipients")]
    [XmlArrayItem("recipient")]
    public List<string> Recipients { get; set; }

另一方面,如果我的 xml 中的每个收件人都是这样定义的:

 <recipients>
  <recipient>
    <fname><fname>
    <lname><lname>
  </recipient>
</recipients>

那么按照我的方式定义一个单独的类是有意义的,即 EmailNoticiationRecipient,但不是将收件人作为属性,而是将 fname 和 lname。

再次感谢@NoIdeaForName :)

最佳答案

正如你所说的那样

[XmlArray("attachments")]
[XmlArrayItem("attachment")]
public List<EmailNotificationAttachment> Attachments { get; set; }

EmailNotification 类中,您的 Attachments 类是

[Serializable]
public class EmailNotificationRecipient
{
    public EmailNotificationRecipient()
    {

    }

    [XmlElement("recipient")]
    public string Recipient { get; set; }
}

这意味着您的 xml 应该如下所示:

  <recipients>
    <recipient><recipient>user1@company.com</recipient></recipient>   
    <recipient><recipient>user2@company.com</recipient></recipient> 
  </recipients>

因为在每个 recipient 标签中都有一个类并且在每个 recipient 类中应该是一个属性名称(标记为) recipient

检查它的最佳方法是创建一个具有值的类并将它们序列化,然后查看输出的样子

关于c# - 反序列化数组总是为元素返回 null 但适用于属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26758142/

相关文章:

c# - 文件的磁盘结构不受支持的错误...找到 11.1,支持 12.0

.net - ASP .Net 服务器控制事件顺序

c# - 将 MDI 子项中的菜单项合并到容器的菜单中

javascript - JS 中的展开/休息运算符如何工作?

c# - 将一个 HttpRequest 中的多个事件发布到 Google Analytics

c# - 验证绑定(bind)到同一对象的不同属性的控件会重置对象值

c# - ASP.NET MVC3 中的 Razor 和接口(interface)继承 : why can't this property be found?

.net - .Net 中的异步 BinaryReader 和 BinaryWriter?

javascript - 如何通过关键词获取最相关的问题

javascript - 切片数组 - "unexpected item in the bagging area?"