XML序列化到许多节点

时间:2014-06-18 20:45:57

标签: c# xml-serialization

我正在寻找以下结构

<EmailAddresses>
    <Email>email@email.com</Email>
    <Email>email@email.com</Email>
</EmailAddresses>

这是我试过的

[XmlRoot("EmailAddresses")]
public class EmailAddress
{
    [XmlElement("Email")]
    public string Email { get; set; }
}

书写

XmlSerializer writer = new XmlSerializer(typeof(List<EmailAddress>), new XmlRootAttribute("EmailAddresses"));
using (FileStream fs = File.Create(filename))
{
    writer.Serialize(fs, emailAddresses);
}

我当前的输出

<EmailAddresses xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <EmailAddress>
        <Email>something here</Email>
    </EmailAddress>
    <EmailAddress>
        <Email>something here</Email>
    </EmailAddress>
</EmailAddresses>

一到多个节点

1 个答案:

答案 0 :(得分:-2)

想出来

[XmlRoot("EmailAddresses")]
public class EmailAddress
{
    [XmlElement("Email")]
    public List<string> Emails { get; set; }
}