Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 反序列化数组总是为元素返回null,但与属性一起使用_C#_.net_Arrays_Serialization_Deserialization - Fatal编程技术网

C# 反序列化数组总是为元素返回null,但与属性一起使用

C# 反序列化数组总是为元素返回null,但与属性一起使用,c#,.net,arrays,serialization,deserialization,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.co

当反序列化部分工作时,我遇到了一个问题。当我有一个带有属性的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>
我不想麻烦显示附件类,因为它作为收件人类的定义是相同的

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

调试时,我可以看到我的数组中有2个用于收件人的项和3个用于附件的项,但当我检查其中的值时,每个数组项都为null

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

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

最初,我使用以下类型定义了
XmlArrayItem

    [XmlArrayItem("recipient", typeof(EmailNotificationRecipient))]
    public List<EmailNotificationRecipient> Recipients { get; set; }
[XmlArrayItem(“收件人”,类型(EmailNotificationRecipient))]
公共列表收件人{get;set;}
我把它取了下来,看看是否会有什么不同,但没有用

我错过了什么?这让我发疯了

谢谢

更新

请参阅下面@NoIdeaForName的答案

我目前定义它的方式意味着,为了让它返回一个值,我的xml应该如下所示:

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

由于我的数组需要一个类收件人,而实际上每个收件人节点中只有一个字符串,因此数组的定义如下:

[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>();
    }
}
    [XmlArray("recipients")]
    [XmlArrayItem("recipient")]
    public List<string> Recipients { get; set; }
[XmlArray(“收件人”)]
[XmlArrayItem(“收件人”)]
公共列表收件人{get;set;}
另一方面,如果xml中的每个收件人的定义如下:

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

然后,按照我的方式定义一个单独的类(即EmailNotificationRecipient)是有意义的,但是您将使用fname和lname,而不是将recipient作为属性


再次感谢@NoIdeaForName:)

正如你所说的那样

[XmlArray("attachments")]
[XmlArrayItem("attachment")]
public List<EmailNotificationAttachment> Attachments { get; set; }
这意味着您的xml应该如下所示:

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

user1@company.com   
user2@company.com 
因为在每个
recipient
标记中都有一个类,并且在每个
recipient
类中都应该有一个属性名(标记为)
recipient


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

EmailNotificationAttachment可序列化吗?如果不是,那就是你的问题。此外,收件人标记的内部不在任何标记中,因此我认为反序列化程序不知道如何处理它。我会写一个答案,但我无法在ATM机上检查它,所以让我知道发生了什么,以便我知道是否要删除它。我可能是正确的。我之前考虑过这一点,因为我的代码在遍历附件时有类似attachment.attachment的内容。我真的不喜欢这个。我会去研究,并试图找出我如何处理这个问题,因为我需要保持xml的原样。我刚刚检查过,你是正确的。我很困惑,因为我认为我定义类的方式是正确的???好的,我修正了它!我将列表更改为字符串类型,而不是特定对象。我想这是有道理的,因为我在接收方节点中只有一个值。如果每个收件人都有子元素,即姓名、名字等。。。创建包含这些属性的类并有一个收件人列表而不是包含这些属性是有意义的。再次感谢您强调这一点!我会更新我的answer@Thierry是的,我正要告诉你一个字符串列表就行了。无论如何,如果是这样的话,请接受我的回答
[Serializable]
public class EmailNotificationRecipient
{
    public EmailNotificationRecipient()
    {

    }

    [XmlElement("recipient")]
    public string Recipient { get; set; }
}
  <recipients>
    <recipient><recipient>user1@company.com</recipient></recipient>   
    <recipient><recipient>user2@company.com</recipient></recipient> 
  </recipients>