将Xml反序列化为Object时出错

时间:2015-04-29 08:43:16

标签: c# xml deserialization

我需要将xml从字符串反序列化为我的类的对象EventOut,我在打开xml时遇到问题。 下面是我的xml:

<app:data xmlns:app="http://mib.test.com/cle/pivot/event">
    <app:EventOut xmlns:ns2="http://mib.test.com/cle/utils/components">
        <app:eventId>20011011</app:eventId>
        <app:distributorId>SOME_VALUE</app:distributorId>
        <app:distributionNetworkId>SOME_VALUE</app:distributionNetworkId>
        <app:typology>SOME_VALUE</app:typology>
        <app:subTypology/>
        <app:targets>
            <app:target>
                <ns2:id>SOME_VALUE</ns2:id>
                <ns2:type>SOME_VALUE</ns2:type>
                <ns2:firstName>SOME_VALUE</ns2:firstName>
                <ns2:secondName>SOME_VALUE</ns2:secondName>
            </app:target>
        </app:targets>
        <app:object>
            <ns2:externalEventObject>
                <ns2:id>616</ns2:id>
                <ns2:lang1>pl</ns2:lang1>
                <ns2:desc1>CCAP</ns2:desc1>
            </ns2:externalEventObject>
        </app:object>
        <app:actorId>SOME_VALUE</app:actorId>
        <app:callerCode>SOME_VALUE</app:callerCode>
        <app:channelCode>SOME_VALUE</app:channelCode>
        <app:comment>SOME_VALUE</app:comment>
        <app:creationDate>2015-04-20T11:22:46+02:00</app:creationDate>
        <app:endTimestamp>2015-04-20T11:22:46+02:00</app:endTimestamp>
        <app:externalId>krl-ext-id-E999-4</app:externalId>
        <app:externalLinkId/>
        <app:modificationDate>2015-04-20T11:22:46+02:00</app:modificationDate>
        <app:operationalStructureId>XXX</app:operationalStructureId>
        <app:startTimestamp>2015-04-20T11:22:46+02:00</app:startTimestamp>
        <app:treatment/>
        <app:customParameters/>
    </app:EventOut>
</app:data>

这是我的班级:

[XmlRoot(ElementName = "EventOut", Namespace = "http://mib.test.com/cle/pivot/event")]
    public class EventOut
    {
        #region app:EventOut
        [XmlElement("app:eventId")]
        private string eventId = string.Empty;

        [XmlElement("app:distributorId")]
        private string distributorId = string.Empty;

        [XmlElement("app:distributionNetworkId")]
        private string distributionNetworkId = string.Empty;

        [XmlElement("app:typology")]
        private string typology = string.Empty;
}

我的反序列化方法:

public static EventOut XmlStringToObject(String XmlString)
        {
            XmlSerializer serial = new XmlSerializer(typeof(EventOut));
            return (EventOut)serial.Deserialize(new StringReader(XmlString));
        }

我有错误

  

'app'是未声明的前缀。

我做错了吗?

0 个答案:

没有答案