Removing XML string deserialization into class

I am trying to deserialize an XML string into my class that is derived from another class, but I have a problem, I get the following error:

{"The specified type is abstract: name='DeviceRequest', namespace='', at <DeviceRequest xmlns=''>."}

I assume that I am missing some decorator attribute that will inform the serializer about the deserialization of this xml in the class?

Here is my code:

 //Usage DeviceRequest dreq = DeviceRequest.ParseRequest(e.XML); //Base Class public abstract class IFSFRequestBase { private string m_ApplicationSender = ""; private int m_WorkStationID = 0; private string m_RequestID = ""; [XmlAttribute()] public abstract string RequestType { get; set; } public abstract byte[] Message(); [XmlAttribute()] public string ApplicationSender { get { return m_ApplicationSender; } set { m_ApplicationSender = value; } } [XmlAttribute()] public int WorkStationID { get { return m_WorkStationID; } set { m_WorkStationID = value; } } [XmlAttribute()] public string RequestID { get { return m_RequestID; } set { m_RequestID = value; } } } //Derived Class public abstract class DeviceRequest : IFSFRequestBase { private string m_TerminalID = ""; private string m_SequenceID = ""; private Output m_OutputField = null; [XmlAttribute(), DefaultValue("")] public string TerminalID { get { return m_TerminalID; } set { m_TerminalID = value; } } [XmlAttribute(), DefaultValue("")] public string SequenceID { get { return m_SequenceID; } set { m_SequenceID = value; } } [XmlElement("Output")] public Output OutputField { get { return m_OutputField; } set { m_OutputField = value; } } public static DeviceRequest ParseRequest(string sXML) { XmlSerializer serializer = new XmlSerializer(typeof(DeviceRequest)); StringReader sr = new StringReader(sXML); NamespaceIgnorantXmlTextReader XMLWithoutNamespace = new NamespaceIgnorantXmlTextReader(sr); return (DeviceRequest)serializer.Deserialize(XMLWithoutNamespace); } } // helper class to ignore namespaces when de-serializing public class NamespaceIgnorantXmlTextReader : XmlTextReader { public NamespaceIgnorantXmlTextReader(System.IO.TextReader reader) : base(reader) { } public override string NamespaceURI { get { return ""; } } } 

UPDATE:

OK, based on the answers here. I updated the code, now I have a Display class that comes from DeviceRequest. Now I get the following error:

 {"There is an error in XML document (2, 2)."}, {"<DeviceRequest xmlns=''> was not expected."} public class Display : DeviceRequest { public static Display ParseRequest(string sXML) { XmlSerializer serializer = new XmlSerializer(typeof(Display)); StringReader sr = new StringReader(sXML); NamespaceIgnorantXmlTextReader XMLWithoutNamespace = new NamespaceIgnorantXmlTextReader(sr); return (Display)serializer.Deserialize(XMLWithoutNamespace); } [XmlAttribute()] public override string RequestType { get { return "Output"; } set { } } public override byte[] Message() { throw new NotImplementedException(); } } DeviceRequest dreq = Display.ParseRequest(e.XML); 
+7
c #
source share
2 answers

OK, I solved this problem now. Thanks for the input guys.

I needed to add:

 [System.Xml.Serialization.XmlRootAttribute(ElementName = "DeviceRequest")] 

for display class

 [System.Xml.Serialization.XmlRootAttribute(ElementName = "DeviceRequest")] public class Display : DeviceRequest { public static Display ParseRequest(string sXML) { XmlSerializer serializer = new XmlSerializer(typeof(Display)); StringReader sr = new StringReader(sXML); NamespaceIgnorantXmlTextReader XMLWithoutNamespace = new NamespaceIgnorantXmlTextReader(sr); return (Display)serializer.Deserialize(XMLWithoutNamespace); } [XmlAttribute()] public override string RequestType { get { return "O"; } set { } } public override byte[] Message() { throw new NotImplementedException(); } } 
+2
source share

Since DeviceRequest is an abstract type, it cannot be DeviceRequest directly. It is not possible to directly deserialize in cases of Device-Request . However, if there are some non-abstract classes derived from it, please show some of them.

+3
source share

All Articles