11

I am trying to change the root name when doing XML serialization with C#.

It always takes the class name and not the name I am trying to set it with.

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {


            MyTest test = new MyTest();
            test.Test = "gog";

            List<MyTest> testList = new List<MyTest>() 
            {    
                test 
            }; 

            SerializeToXML(testList);
        }

        static public void SerializeToXML(List<MyTest> list)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<MyTest>));
            TextWriter textWriter = new StreamWriter(@"C:\New folder\test.xml");
            serializer.Serialize(textWriter, list);
            textWriter.Close();
        }
    }


}





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

namespace ConsoleApplication1
{

    [XmlRootAttribute(ElementName = "WildAnimal", IsNullable = false)]
    public class MyTest
    {
        [XmlElement("Test")]
        public string Test { get; set; }


    }
}

Result

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfMyTest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <MyTest>
    <Test>gog</Test>
  </MyTest>
</ArrayOfMyTest>

It does not change it to WildAnimal. I am not sure why. I got this from a tutorial.

Edit @ Marc

Thanks. I now see what your doing just seems so odd that you have to make a wrapper around it. I have one more question what happens if I wanted to make this format

<root>
   <element>
        <name></name>
   </element>
   <anotherElement>
       <product></product> 
   </anotherElement>
</root>

so like a nested elements. Would I have to make a new class for the second part and stick that in the wrapper class too?

1
  • Re your edit; it depends whether you wand one anotherElement per product, or a single anotherElement with lost of product elements. For the latter: [XmlArray] / [XmlArrayItem] - otherwise yes: a second type. Commented Jan 24, 2010 at 7:58

1 Answer 1

15

In your example, MyTest is not the root; are you trying to rename the array? I would write a wrapper:

[XmlRoot("NameOfRootElement")]
public class MyWrapper {
    private List<MyTest> items = new List<MyTest>();
    [XmlElement("NameOfChildElement")]
    public List<MyTest> Items { get { return items; } }
}

static void Main() {
    MyTest test = new MyTest();
    test.Test = "gog";

    MyWrapper wrapper = new MyWrapper {
        Items = {  test }
    };
    SerializeToXML(wrapper);
}

static public void SerializeToXML(MyWrapper list) {
    XmlSerializer serializer = new XmlSerializer(typeof(MyWrapper));
    using (TextWriter textWriter = new StreamWriter(@"test.xml")) {
        serializer.Serialize(textWriter, list);
        textWriter.Close();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I guess I am trying to change the name of the "ArrayOfMyTest" I thought MyTest was the root and that was just something the serialization needed. So what do I do with this wrapper? Like how do I take it now and serialize it?
@chobo2 - I've provided a full example with Main, SerializeToXML and MyWrapper.

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.