开发者

Defining XmlRoot and typeof it with a variable?

开发者 https://www.devze.com 2023-01-05 07:28 出处:网络
[XmlRoot(\"ConfigurationRoot\")] public class XmlDBConfiguration { [XmlArray(\"Customers\")] [XmlArrayItem(\"Customer\", typeof(Customer))]
[XmlRoot("ConfigurationRoot")]
public class XmlDBConfiguration
{
    [XmlArray("Customers")]
    [XmlArrayItem("Customer", typeof(Customer))]

    public ArrayList _customers;
    private Dictionary<string, Customer> _customerDictionary;}

Is it possible to define the Element Name (in my case it is ConfigurationRoot) with a variable from another static class or any other way? I want to define my class that is going to be serialized (in my case it is Customer) as well. Is it possible to put a varia开发者_开发技巧ble there and change it when I create an instance of that ?

Thanks in advance


Yes, you can do that by using the XmlSerializer constructor that takes a XmlAttributesOverrides object. Providing your own XmlRootAttribute is explicitly supported. It is all well explained in the MSDN Library topic.


I don't think you can change the value of the XmlRoot attribute on your class. You can however specify an override of this attribute when you create XmlSerializer - just use this constructor: http://msdn.microsoft.com/en-us/library/f1wczcys.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号