Search code examples
c#serializationxml-serialization

How do I change the XmlElement name dynamically?


[XmlRoot("A")]
public class Foos
{
   [XmlElement("A1")]
   public List<Foo> FooList{ get; set; }
}


var serializer = new XmlSerializer(typeof(Foos));

This code working as well. But its not dynamic. I want [XmlRoot("A")] to [XmlRoot(ConfigurationManager.AppSettings[someValue])]. But is throw to syntax error. Then i try this

    public class Foos
    {
       [XmlElement("A1")]
       public List<Foo> FooList{ get; set; }
    }
var serializer = new XmlSerializer(typeof(Foos),new XmlRootAttribute(ConfigurationManager.AppSettings[someValue]));

This is work just root element.I'm working. I could not change the "XmlElement" value of the FooList dynamically.There can be more than one element in the class. How do I change the XmlElement value of all of them dynamically?


Solution

  • You need to use XmlAttributesOverrides in a right way. Please Check.

    Working version of your code is here.

    public class Foos
    {       
        public List<Foo> FooList { get; set; }
    }
    
    
    public class Foo
    {
        public string Name { get; set; }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
    
            var xmlAttributeForFoos = new XmlAttributes { XmlRoot = new XmlRootAttribute(ConfigurationManager.AppSettings["someFoosValue"]), XmlType = new XmlTypeAttribute(ConfigurationManager.AppSettings["someFoosValue"]) };
            var xmlAttributeForFooList = new XmlAttributes();
            var xmlAttributeForFoo = new XmlAttributes();
    
            xmlAttributeForFooList.XmlElements.Add(new XmlElementAttribute(ConfigurationManager.AppSettings["someFooValue"]));
            xmlAttributeForFoo.XmlElements.Add(new XmlElementAttribute(ConfigurationManager.AppSettings["someFooNameValue"]));
    
             var overrides = new XmlAttributeOverrides();
            overrides.Add(typeof(Foos), xmlAttributeForFoos);
            overrides.Add(typeof(Foos), "FooList", xmlAttributeForFooList);
            overrides.Add(typeof(Foo), "Name", xmlAttributeForFoo);
    
            XmlSerializer serializer = new XmlSerializer(typeof(Foos), overrides);
    
            var foos = new Foos
            {
                FooList = new List<Foo>
                {
                    new Foo{Name = "FooName"}
                }
            };
    
            using (var stream = File.Open("file.xml", FileMode.OpenOrCreate))
            {
                serializer.Serialize(stream, foos);
            }
        }
    }
    

    App settings

    <appSettings>
      <add key="someFoosValue" value="SomeFoos"/>    
      <add key="someFooValue" value="SomeFoo"/>
      <add key="someFooNameValue" value="FooName"/>
    </appSettings>
    

    output

    <SomeFoos xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <SomeFoo>
        <FooName>FooName</FooName>
      </SomeFoo>
    </SomeFoos>