0

I have referenced this question: 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>[email protected]</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>

This is what I want my XML to look like. How can I change the code below to build it in this fashion.

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

This is my class definition to serialize:

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;
    }
}

This is my code to write out the serialized object to an XML file:

    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 = "[email protected]";
        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(); 

    }

Thanks.

1 Answer 1

3

You can use the [XmlElement] attribute to specify that your List<string> should be serialized without an outer wrapper element:

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;
}

Doing so will cause the list to be serialized as a sequence of elements each having the same name as the list property being serialized, in this case TextContains.

If you had needed to override the element name, it can be specified in the attribute constructor:

    [XmlElement("Contains")]
    public List<string> TextContains;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I thought I tried that, but maybe I was making too many changes at once.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.