将XML字符串反序列化为类

时间:2014-12-15 12:44:23

标签: c#

我正在尝试将XML String反序列化到我的类中,该类派生自另一个类但是我遇到了这样的问题,我收到以下错误:

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

我假设我缺少一些decorator属性,它会告诉序列化程序如何将这个xml反序列化到类中?

这是我的代码:

//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 ""; }
    }
}

更新:

根据这里的答案确定。我已经更新了代码,现在我有一个派生自DeviceRequest的类Display。我现在收到以下错误:

{"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);

2 个答案:

答案 0 :(得分:3)

由于DeviceRequest是抽象类型,因此无法直接实例化。直接反序列化为Device-Request的实例是不可能的。也就是说,如果有一些非抽象类派生自它,请展示其中一些。

答案 1 :(得分:2)

好的,我现在已经解决了这个问题。感谢输入人员。

我需要添加:

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

到显示类

[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();
    }
}