C#如何将字符串数组序列化/反序列化为一系列命名元素

时间:2015-10-06 16:37:24

标签: .net c#-4.0 xml-serialization xmlserializer

我引用了这个问题:Using XmlSerializer with an array in the root element

<?xml version="1.0" encoding="utf-8"?>
<Subscribers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <SubscriberList>
    <Subscriber>
      <Email>test@somewhere.com</Email>
      <SubscriptionList>
        <Subscription>
          <Source>TFSBuildServiceHost.2013</Source>
        </Subscription>
        <Subscription>
          <TextContains>
            <string>ABC</string>
            <string>DEF</string>
            <string>XYZ</string>
          </TextContains>
        </Subscription>
      </SubscriptionList>
    </Subscriber>
  </SubscriberList>
</Subscribers>

这就是我希望我的XML看起来像。如何更改下面的代码以这种方式构建它。

        <Subscription>
            <TextContains>ABC</TextContains>
            <TextContains>DEF</TextContains>
            <TextContains>XYZ</TextContains>
        </Subscription>

这是我要序列化的类定义:

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

namespace EventLog_Reporter
{
    public class Subscription
    {
        public string Source;
        public List<string> TextContains;
        public List<string> UCTextContains;
        public List<string> TextExcludes;
        public List<string> UCTextExcludes;
    }

    public class Subscriber
    {
        public string Group;
        public string Email;
        public List<Subscription> SubscriptionList; 
    }

    public class Subscribers 
    {
        public List<Subscriber> SubscriberList;
    }
}

这是我将序列化对象写出到XML文件的代码:

    public static void testSerialization()
    {
        Subscribers subscribers = new Subscribers();
        Subscription subscription1 = new Subscription();
        Subscription subscription2 = new Subscription();
        Subscriber subscriber1 = new Subscriber();

        subscription2.TextContains = new List<string>(); 
        subscription2.TextContains.Add("ABC"); 
        subscription2.TextContains.Add("DEF"); 
        subscription2.TextContains.Add("XYZ"); 

        subscription1.Source = "TFSBuildServiceHost.2013";

        subscriber1.Email = "test@somewhere.com";
        subscriber1.SubscriptionList = new List<Subscription>(); 
        subscriber1.SubscriptionList.Add(subscription1);
        subscriber1.SubscriptionList.Add(subscription2);

        subscribers.SubscriberList = new List<Subscriber>();
        subscribers.SubscriberList.Add(subscriber1);

        string strOutFilenameXMLSerializer = "xmlSerializerObjectTest1.xml"; 
        StreamWriter sw = new StreamWriter(strOutFilenameXMLSerializer);
        XmlSerializer xmlSerializer = new XmlSerializer(subscribers.GetType());
        xmlSerializer.Serialize(sw, subscribers);
        sw.Close(); 

    }

感谢。

1 个答案:

答案 0 :(得分:2)

您可以使用[XmlElement]属性指定在没有外部包装元素的情况下序列化List<string>

public class Subscription
{
    public string Source;
    [XmlElement]
    public List<string> TextContains;
    [XmlElement]
    public List<string> UCTextContains;
    [XmlElement]
    public List<string> TextExcludes;
    [XmlElement]
    public List<string> UCTextExcludes;
}

这样做会导致列表被序列化为一系列元素,每个元素都与被序列化的列表属性具有相同的名称,在本例中为TextContains

如果您需要覆盖元素名称,可以在属性构造函数中指定:

    [XmlElement("Contains")]
    public List<string> TextContains;