I started with the following code:
public class ChangeRestartProcessing
{
[XmlElement("ID")]
public long TransportId { get; set; }
[XmlElement("FI")]
public Information FinalInformation { get; set; }
}
... and while serialising, everything worked fine: FI
was visible in the XML serialisation result.
Now, I've made following modification:
public class ChangeRestartProcessing
{
[XmlElement("ID")]
public long TransportId { get; set; }
[XmlElement("FI")]
public Information FinalInformation { get; set; }
[XmlElement("DI")]
public Information NormalInformation { get; set; }
}
The idea is to see the FI
when that object exists, and to see DI
when that object exists (it never happens that both are present).
However, now I don't see any FI
or DI
tag in my XML serialisation result.
Is that normal and what can I do in order to make the mentioned tags visible? Do I need to create a separate class for both cases or is there another approach?
CodePudding user response:
Works fine here...
using System;
using System.Xml.Serialization;
var serializer = new XmlSerializer(typeof(ChangeRestartProcessing));
serializer.Serialize(Console.Out, // has ID FI only
new ChangeRestartProcessing
{
FinalInformation = new() { Id = 42 },
});
Console.WriteLine();
serializer.Serialize(Console.Out, // has ID DI only
new ChangeRestartProcessing
{
NormalInformation = new() { Id = 42 },
});
public class ChangeRestartProcessing
{
[XmlElement("ID")]
public long TransportId { get; set; }
[XmlElement("FI")]
public Information FinalInformation { get; set; }
[XmlElement("DI")]
public Information NormalInformation { get; set; }
}
public class Information
{
[XmlAttribute]
public int Id { get; set; }
}
output:
<?xml version="1.0" encoding="Codepage - 850"?>
<ChangeRestartProcessing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ID>0</ID>
<FI Id="42" />
</ChangeRestartProcessing>
<?xml version="1.0" encoding="Codepage - 850"?>
<ChangeRestartProcessing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ID>0</ID>
<DI Id="42" />
</ChangeRestartProcessing>