Hi guys I have 2 classes like this:
[XmlRoot("Config")]
public class ConfigClass
{
[XmlElement("Configuration1")]
public string Config1 { get; set; }
[XmlArray("Infos")]
[XmlArrayItem("Info")]
public OtherInfo[] OtherInfos { get; set; }
}
public class OtherInfo
{
public string Info1 { get; set; }
public string Info2 { get; set; }
}
When I serialize the root class the XML result is like this:
<?xml version="1.0"?>
<Config>
<Configuration1>Text</Configuration1>
<Infos>
<Info>
<Info1>Test 2</Info1>
<Info2>Text 3</Info2>
</Info>
<Info>
<Info1>Test 4</Info1>
<Info2>Text 5</Info2>
</Info>
</Infos>
</Config>
But I would like the OtherInfo
class was serialized as a single string like this:
<?xml version="1.0"?>
<Config>
<Configuration1>Text</Configuration1>
<Infos>
<Info>
Test 2:Text 3
</Info>
<Info>
Test 4:Text 5
</Info>
</Infos>
</Config>
How I can do that?
You can implement the IXmlSerializable interface, so the
Info1
andInfo2
properties are serialized the way<Info>Info1:Info2</Info>
: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: