You can implement the IXmlSerializable interface, so the Info1
and Info2
properties are serialized the way <Info>Info1:Info2</Info>
:
public class OtherInfo: IXmlSerializable
{
public string Info1 { get; set; }
public string Info2 { get; set; }
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(System.Xml.XmlReader reader)
{
var content = reader.ReadElementContentAsString();
if (String.IsNullOrWhiteSpace(content))
return;
var infos = content.Split(':');
if (infos.Length < 2)
return;
this.Info1 = infos[0];
this.Info2 = infos[1];
}
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteString(String.Format("{0}:{1}", this.Info1, this.Info2));
}
}
If having those properties in the format "Info1:Info2" is also needed inside the application and not just for Xml serialization, then you could have a property in OtherInfo
like the following and hide Info1 and Info2 from the serialization:
public class OtherInfo
{
[XmlIgnore]
public string Info1 { get; set; }
[XmlIgnore]
public string Info2 { get; set; }
[XmlText]
public string InfoString
{
get
{
return String.Format("{0}:{1}", this.Info1, this.Info2);
}
set
{
if (String.IsNullOrWhiteSpace(value))
return;
var infos = value.Split(':');
if (infos.Length < 2)
return;
this.Info1 = infos[0];
this.Info2 = infos[1];
}
}
}