I have a class with two parameters. One is the update time in Minutes and the other is the same value in seconds like this:
using System.Xml.Serialization;
[XmlRoot(ElementName="config")]
public class Config
{
[XmlElement(ElementName = "update_mins")]
public int Minutes
{
get
{
return this.Seconds / 60;
}
set
{
this.Seconds = value * 60;
}
}
[XmlElement(ElementName = "update_secs")]
public int Seconds { get; set; }
}
I get xml strings that can have either update_mins or update_secs but not both. I need to be able to use this class to deserialize these strings, this works ok. I also need to be able to searalize these classes and send them on, however when I do the serailzation it includes both update_secs and update_mins. Like this:
<config>
<update_mins>23</update_mins>
<update_secs>1380</update_secs>
</config>
When I want to get this:
<config>
<update_secs>1380</update_secs>
</config>
I tried putting XmlIgnore on the update_mins value, but that stopped it deserializing the element. Is it possible to do this, and if so how?
My full code example is:
namespace Scrap
{
class Program
{
static void Main(string[] args)
{
string s = "<config><update_mins>23</update_mins></config>";
XmlSerializer searializer = new XmlSerializer(typeof(Config));
Config result = (Config)searializer.Deserialize(new StringReader(s));
Console.WriteLine("Minutes: " + result.Minutes);
Console.WriteLine("Seconds: " + result.Seconds);
Debug.Assert(result.Minutes == 23);
Debug.Assert(result.Seconds == 1380);
StringWriter s2w = new StringWriter();
searializer.Serialize(s2w, result);
Console.WriteLine(s2w.ToString());
string xmlResult = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n" +
"<config xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\r\n" +
" <update_secs>1380</update_secs>\r\n" +
"</config>";
Debug.Assert(s2w.ToString() == xmlResult);
Console.ReadKey(true);
}
}
[XmlRoot(ElementName = "config")]
public class Config
{
[XmlElement(ElementName = "update_mins")]
public int Minutes
{
get
{
return this.Seconds / 60;
}
set
{
this.Seconds = value * 60;
}
}
[XmlElement(ElementName = "update_secs")]
public int Seconds { get; set; }
}
}
This is still not an ideal solution, but the way I have resolved this is to add [XmlIgnore] to the Minutes property (as Aliostad and others had suggested) and then add an AllElements property like this: