How to serialize xml element to class without corresponding class element in c#

Viewed 20

I can serialize the following to my custom c# classes:

<Servers>
    <Server url="www.temp.de">
        <Config>
            <Log>
                <Path>C:/temp</Path>
                <Level>DEBUG</Level>
            </Log>
            <Filter>
                <Filter>Created</Filter>
                <Filter>Modified</Filter>
            </Filter>
        </Config>
    </Server>
</Servers>

The <Config> section for <Server> and <Application> has the same structure, so I use the following classes to serialize the server:

[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class Server
{
    private string url;
    private Configuration configuration;

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string URL { // get & set }

    public Configuration Config { // get & set }
}

[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class Configuration
{
    private LogConfiguration logConfiguration;
    private FilterConfiguration filterConfiguration;

    public LogConfiguration Log { // get & set }
    public FilterConfiguration Filter { // get & set }    
}

Now I have an xml without the <Config> tag. <Log> and <Filter> are directly under <Server>.

Don't know how to use an annotation in Server-Class to tell the Configuration-Object Config itself can not be serialized by corresponding tag but the subproperties of the Configuration-Class can be serialized by corresponding tags.

Is there an annotion based solution for that?

0 Answers
Related