I am InvalidOperationException when I try to run this code.
Code example
using System; using System.Collections.Generic; using System.IO; using System.Xml.Serialization; namespace XMLSerializationExample { class Program { static void Main(string[] args) { Caravan c = new Caravan(); c.WriteXML(); } } [XmlRoot("caravan", Namespace="urn:caravan")] public class Caravan { [XmlElement("vehicle")] public Auto Vehicle; public Caravan() { Vehicle = new Car { Make = "Suzuki", Model = "Swift", Doors = 3 }; } public void WriteXML() { XmlSerializer xs = new XmlSerializer(typeof(Caravan)); using (TextWriter tw = new StreamWriter(@"C:\caravan.xml")) { xs.Serialize(tw, this); } } } public abstract class Auto { public string Make; public string Model; } public class Car : Auto { public int Doors; } public class Truck : Auto { public int BedLength; } }
Internal exception
{"The XMLSerializationExample.Car type was not expected. Use the XmlInclude or SoapInclude attribute to indicate types that are not known statically." }
Question
How do I fix this code? Is there anything else I should do?
Where can I put the following?
[XmlInclude(typeof(Car))] [XmlInclude(typeof(Truck))]
Putting attributes above the Auto or Caravan classes does not work. Adding types directly to the XmlSerializer , as in the example below, also does not work.
XmlSerializer xs = new XmlSerializer(typeof(Caravan), new Type[] { typeof(Car), typeof(Truck) });
kzh
source share