I have the following code:
[Serializable]
[XmlRoot("Database")]
public class SqlDatabase
{
public List<SqlTable> Tables { get; private set; }
}
If I use XmlSerializer
without any custom attribute, generated XML persists list hierarchy:
<Database>
<Tables>
<SqlTable Name="Table1" />
<SqlTable Name="Table2" />
</Tables>
</Database>
However, I want to change the element name "SqlTable" to "Table". I tried to use XmlElement
attribute on the list
[Serializable]
[XmlRoot("Database")]
public class SqlDatabase
{
[XmlElement("Table")]
public List<SqlTable> Tables { get; private set; }
}
The name is changed but the hierarchy is flattened:
<Database>
<Table Name="Table1" />
<Table Name="Table2" />
</Database>
Then I tried XmlArray
attribute, which changes the name of the list but not the elements within:
[Serializable]
[XmlRoot("Database")]
public class SqlDatabase
{
[XmlArray("Foo")]
public List<SqlTable> Tables { get; private set; }
}
Results in
<Database>
<Foo>
<SqlTable Name="Table1" />
<SqlTable Name="Table2" />
</Foo>
</Database>
I tried to combine these two and there is an exception saying they can't be used together.
So the question is, is there a simple way to change the element name without flattening the hierarchy?