Don Rhummy
Don Rhummy

Reputation: 25820

How would I achieve the following XmlSerializer change for my class in C#?

I want to produce the following XML:

<Base>
    <Child>
        <Name>Joe</Name>
    </Child>
    <Child>
        <Name>Jack</Name>
    </Child>
</Base>

From the classes:

public class Base
{
    public List<Child> children;
    ...elided...
}

public class Child
{
    public string Name;
    ...elided...
}

Right now, it's creating:

<Base>
    <children>
        <Child>
            <Name>Joe</Name>
        </Child>
        <Child>
            <Name>Jack</Name>
        </Child>
    </children>
</Base>

How would I change to produce the desired output?

My current code:

XmlSerializer serializer = new XmlSerializer(base.GetType());
serializer.serialze(stringWriter, base);
return stringWriter.ToString();

Upvotes: 2

Views: 52

Answers (3)

Chris Berger
Chris Berger

Reputation: 555

Edit: The other solutions are better and more direct answers to your question. However, I'm going to leave this here, as extending List<> rather than including a public List member can be a good idea in some cases. And it is possible solution.

Well, you could implement IXmlSerializable, but that might be overkill for what you're looking to do. Alternately, you could make Base extend List<Child>.

public class Base : List<Child>
{
}

This will, of course, change how you reference the children in the rest of your code. If you had base.children[0], you would instead use base[0].

Upvotes: 0

Matthew Whited
Matthew Whited

Reputation: 22433

Use the XmlElementAttribute

public class Base
{
    [XmlElement(ElementName = "Child")]
    public List<Child> children;
}

... full example ...

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

class Program
{
    static void Main()
    {
        var xsr = new XmlSerializer(typeof(Base));

        var b = new Base
        {
            children = new List<Child>
                {
                    new Child { Name= "Joe"},
                    new Child { Name ="Jack"},
                }
        };
        using (var ms = new MemoryStream())
        {
            xsr.Serialize(ms, b);

            var str = Encoding.UTF8.GetString(ms.ToArray());
            /*
            <?xml version="1.0"?>
            <Base xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
              <Child>
                <Name>Joe</Name>
              </Child>
              <Child>
                <Name>Jack</Name>
              </Child>
            </Base>
             */
        }
    }
}

public class Base
{
    [XmlElement("Child")]
    public List<Child> children;
}

public class Child
{
    public string Name;
}

Upvotes: 4

P. Kouvarakis
P. Kouvarakis

Reputation: 1943

You can use the XmlElementAttribute as follows:

public class Base
{
    [XmlElement("Child")]
    public List<Child> children;
    ...elided...
}

public class Child
{
    public string Name;
    ...elided...
}

Upvotes: 2

Related Questions