从XmlSerializer

时间:2015-07-29 02:38:14

标签: c# xml xmlserializer

我正在使用XmlSerializer类将对象序列化为xml文件并通过线路传输。 我创建了以下数据结构,其中包含内容数据并通过线路传输。

 [Serializable]
 public class MailItem
{
    public string UniqueEmailId { get; set; }
    public string SenderEmailId { get; set; }
    public string[] ToRecipientEmailId { get; set; }
}

现在当我转换为xml时,它会发出以下结果 -

<?xml version="1.0" encoding="utf-16"?>
<MailItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <UniqueEmailId>SomeUniqueID</UniqueEmailId>
  <SenderEmailId>sender@email.id</SenderEmailId>
  <ToRecipientEmailId>
    <string>To@email.id</string>
    <string>To1@email.id</string>
    <string>To2@email.id</string>
  </ToRecipientEmailId>  
</MailItem>

预期结果是(观察ToRecipientEmailId的子元素) -

<?xml version="1.0" encoding="utf-16"?>
<MailItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <UniqueEmailId>SomeUniqueID</UniqueEmailId>
  <SenderEmailId>sender@email.id</SenderEmailId>
  <ToRecipientEmailId>
    <EmailId>To@email.id</EmailId>
    <EmailId>To1@email.id</EmailId>
    <EmailId>To2@email.id</EmailId>
  </ToRecipientEmailId>  
</MailItem>

当我创建另一个使用Email和EmailId作为属性的类时,它会给我以下结果 -

[Serializable]
     public class MailItem
    {
        public string UniqueEmailId { get; set; }
        public string SenderEmailId { get; set; }
        public List<Email> ToRecipientEmailId { get; set; }
    }         

   public class Email
    {
        public string EmailID {get;set;}
    }

<?xml version="1.0" encoding="utf-16"?>
<MailItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <UniqueEmailId>SomeUniqueID</UniqueEmailId>
  <SenderEmailId>sender@email.id</SenderEmailId>
  <ToRecipientEmailId>
    <Email>
      <EmailID>To@email.id</EmailID>
    </Email>
    <Email>
      <EmailID>To1@email.id</EmailID>
    </Email>
    <Email>
      <EmailID>To2@email.id</EmailID>
    </Email>
  </ToRecipientEmailId> 
</MailItem>

如果有人让我知道如何达到预期效果,那将会很棒。我需要在数据结构中做出哪些更改?

2 个答案:

答案 0 :(得分:1)

您可以使用[XmlArrayItem(elementName)]属性指定要用于数组条目的元素名称:

public class MailItem
{
    public string UniqueEmailId { get; set; }
    public string SenderEmailId { get; set; }
    [XmlArrayItem("EmailId")]
    public string[] ToRecipientEmailId { get; set; }
}

请注意,您不需要用于二进制序列化的[Serializable]属性。

答案 1 :(得分:1)

试试这个。不要使用XmlArrayItem将创建一个额外的标签层

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
    [XmlRoot("MailItem")]
    public class MailItem
    {
        [XmlElement("UniqueEmailId")]
        public string UniqueEmailId { get; set; }
        [XmlElement("SenderEmailId")]
        public string SenderEmailId { get; set; }
        [XmlElement("ToRecipientEmailId")]
        public string[] ToRecipientEmailId { get; set; }
    }
}
​