I use XmlSerializer. My class:
[Serializable]
[XmlRoot(ElementName="MyClass")]
public class MyClass
{
public string Value;
}
I would like to serialize it so that it Valueends as an attribute of a sub-element named (for example) “Text”.
Desired Result:
<MyClass>
<Text Value="3"/>
</MyClass>
But NOT (which would be a consequence of labeling the value as XmlAttribute)
<MyClass Value="3">
</MyClass>
AND NOT (which would be the result of marking the value as XmlElement):
<MyClass>
<Value>3</Value>
</MyClass>
How do I achieve this?
I know that I could change the type Valuefrom a string to another serializable custom class.
Unfortunately, I have many such properties, so I need to create dozens of tiny classes.
Is there a faster solution?
EDIT:
In response to your comments:
!
[XmlRoot(ElementName="Visibility")]
public class Visibility
{
[XPath("/site@visible")]
public string OnSite
{
get { return SiteVisible ? "yes" : "no"; }
}
[XPath("/comparator@visible")]
public string InComparator
{
get { return ComparatorVisible ? "yes" : "no"; }
}
[XmlIgnore]
public bool SiteVisible;
[XmlIgnore]
public bool ComparatorVisible;
[XPath("/expiration@days")]
public int ExpiresAfterDays;
[XmlElement("comment")]
public string Comment;
}