Synster
Synster

Reputation: 339

Is there a way to do the following with XmlSerializer?

I have an Xml file which I want to de-serialize into a class the catch is the properties of class can be written both as a child or attribute of the parent tag?

Sample Xml files I am using :-

<?xml version="1.0" encoding="utf-8" ?>
<Column Name="Limit">
    <Type>String</Type>
</Column>

<?xml version="1.0" encoding="utf-8" ?>
<Column>
    <Name>Limit</Name>
    <Type>String</Type>
</Column>

Any help is much appreciated.

Upvotes: 2

Views: 52

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063774

Well, you could try something like:

[XmlAttribute("Name")]
public string Name {get;set;}

[XmlElement("Name")]
public string NameAlt {
    get { return Name; }
    set { Name = value; }
}
// to prevent serialization (doesn't affect deserialization)
public bool ShouldSerializeNameAlt() { return false; }

Upvotes: 3

Related Questions