15

We have the followig code:

[Serializable]
public class Class1
{
    [XmlElement("description")]
    public string Description { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        var list = new List<Class1> {new Class1() {Description = "Desc1"}, new Class1() {Description = "Desc2"}};
        var serializer = new XmlSerializer(typeof(List<Class1>), new XmlRootAttribute("root"));
        var ms = new MemoryStream();
        serializer.Serialize(ms, list);
        ms.Position = 0;
        var result = new StreamReader(ms).ReadToEnd();
    }
}

after execution we will have the following in 'result' variable:

<?xml version="1.0"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Class1>
    <description>Desc1</description>
  </Class1>
  <Class1>
    <description>Desc2</description>
  </Class1>
</root>

The question is: how to change xml elements name from 'Class1' to 'Item1' without changing class name?

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
Anubis
  • 2,484
  • 2
  • 22
  • 33

2 Answers2

41

You can use XmlTypeAttribute.TypeName for this.

Try this for you Class1 definition

    [XmlType(TypeName = "Item1")]
    [Serializable]
    public class Class1
    {
        [XmlElement("description")]
        public string Description { get; set; }
    }
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • Any way to have a dynamic name generation dependent on the property name? (ie: instead of having [XmlElement("description")] to have some dynamic code that does, say CurrentName.ToLowerCase() ) and Description will be description? – kkica Dec 04 '19 at 02:40
5

Use an XmlTypeAttribute on the class as well:

[XmlType(TypeName="ElementName")]
[Serializable]
public class Class1 { ...

EDIT: Updated from XmlRootAttribute to XmlTypeAttribute. The former works where the type being passed to the serialiser is the attributed type (Class1 here), but not when there is a wrapping type (List<Class1> here). That XmlType works is not clear from the documentation (my emphasis):

Controls the XML schema that is generated when the attribute target is serialized by the XmlSerializer.

Credit to Bala R's answer.

Community
  • 1
  • 1
Richard
  • 106,783
  • 21
  • 203
  • 265
  • I don't think XmlElementAttribute can be applied for classes.`[AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue, AllowMultiple = true)] public class XmlElementAttribute : Attribute` and there is no `AttributeTargets.Class` – Bala R Jun 17 '11 at 11:47
  • The System.Xml.Serialization.XmlElementAttribute has AttributeUsage: Field | Parameter | Property | ReturnValue but not the Type – Anubis Jun 17 '11 at 11:48
  • @TheOtherGuy... it should, look at the sample on [MSDN](http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlrootattribute.aspx)... but that `List` is messing things up. – Richard Jun 17 '11 at 13:33
  • Any way to have a dynamic name generation dependent on the property name? (ie: instead of having [XmlElement("description")] to have some dynamic code that does, say CurrentName.ToLowerCase() ) and Description will be description? – kkica Dec 04 '19 at 02:41