2015-09-18 50 views
1

我需要反序列化符合CBI意大利標準的XML,問題是我已經用命名空間標記了每個類,但仍無法反序列化。C#反序列化CBISDDStsRptPhyMsg

這是我試圖反序列化XML的一部分(內容擦除):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<RISPOSTASDD:CBISDDStsRptPhyMsg xmlns:HE2E="urn:CBI:xsd:CBIHdrSrv.001.07" xmlns:BODY="urn:CBI:xsd:CBIBdySDDStsRpt.00.01.00" xmlns:SGNT="urn:CBI:xsd:CBISgnInf.001.04" xmlns:RISPOSTASDD="urn:CBI:xsd:CBISDDStsRptPhyMsg.00.01.00" xmlns:LMSG="urn:CBI:xsd:CBISDDStsRptLogMsg.00.01.00" xmlns:HTRT="urn:CBI:xsd:CBIHdrTrt.001.07"> 
    <RISPOSTASDD:CBIHdrTrt> 
     <HTRT:IdCBISndrf></HTRT:IdCBISndrf> 
     <HTRT:IdCBIRcvrf></HTRT:IdCBIRcvrf> 
     <HTRT:SrvNm></HTRT:SrvNm> 
     <HTRT:IdMsgTrt></HTRT:IdMsgTrt> 
     <HTRT:XMLCrtDt></HTRT:XMLCrtDt> 
     <HTRT:RtrnAddrl></HTRT:RtrnAddrl> 
    </RISPOSTASDD:CBIHdrTrt> 
    <RISPOSTASDD:CBIHdrSrv> 
     <HE2E:SrvInfo> 
      <HE2E:SrvNm></HE2E:SrvNm> 
      <HE2E:IdE2EMsg></HE2E:IdE2EMsg> 
      <HE2E:XMLCrtDt></HE2E:XMLCrtDt> 
     </HE2E:SrvInfo> 
     <HE2E:Sender> 
      <HE2E:IdCBISend></HE2E:IdCBISend> 
      <HE2E:SendTyp></HE2E:SendTyp> 
      <HE2E:CBIRefrSend></HE2E:CBIRefrSend> 
     </HE2E:Sender> 
     <HE2E:Receiver> 
      <HE2E:IdCBIRecv></HE2E:IdCBIRecv> 
      <HE2E:RecvTyp></HE2E:RecvTyp> 
      <HE2E:CBIRefrRecv></HE2E:CBIRefrRecv> 
     </HE2E:Receiver> 
     <HE2E:DiagInfo> 
      <HE2E:UsrBnk></HE2E:UsrBnk> 
      <HE2E:DiagVers></HE2E:DiagVers> 
      <HE2E:ChkSbj></HE2E:ChkSbj> 
      <HE2E:ChkDt></HE2E:ChkDt> 
     </HE2E:DiagInfo> 
     <HE2E:CongrInfo> 
      <HE2E:SrvBdyNb></HE2E:SrvBdyNb> 
     </HE2E:CongrInfo> 
    </RISPOSTASDD:CBIHdrSrv> 
    <RISPOSTASDD:CBIBdySDDStsRpt> 
     <BODY:PhyMsgInf> 
      <BODY:PhyMsgTpCd></BODY:PhyMsgTpCd> 
      <BODY:NbOfLogMsg></BODY:NbOfLogMsg> 
     </BODY:PhyMsgInf> 
     <BODY:CBIEnvelSDDStsRptLogMsg> 
      <BODY:CBISDDStsRptLogMsg>... 

而這些(部分),我已經寫了類反序列化:

[Serializable, XmlRoot(Namespace = "urn:CBI:xsd:CBISDDStsRptPhyMsg.00.01.00")] 
public class CBISDDStsRptPhyMsg 
{ 
    [XmlElement("CBIHdrTrt", Namespace = "urn:CBI:xsd:CBIHdrTrt.001.07")] 
    public CBIHdrTrt CBIHdrTrt {get;set;} 
    [XmlElement("CBIHdrSrv", Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")] 
    public CBIHdrSrv CBIHdrSrv {get;set;} 
    [XmlElement("CBIBdySDDStsRpt", Namespace="urn:CBI:xsd:CBIBdySDDStsRpt.00.01.00")] 
    public CBIBdySDDStsRpt CBIBdySDDStsRpt { get; set; } 
} 
[Serializable] 
public class CBIHdrTrt 
{ 
    [XmlElement("IdCBISndrf", Namespace = "urn:CBI:xsd:CBIHdrTrt.001.07")] 
    public string IdCBISndrf { get; set; } 
    [XmlElement("IdCBIRcvrf", Namespace = "urn:CBI:xsd:CBIHdrTrt.001.07")] 
    public string IdCBIRcvrf { get; set; } 
    [XmlElement("SrvNm", Namespace = "urn:CBI:xsd:CBIHdrTrt.001.07")] 
    public string SrvNm { get; set; } 
    [XmlElement("IdMsgTrt", Namespace = "urn:CBI:xsd:CBIHdrTrt.001.07")] 
    public string IdMsgTrt { get; set; } 
    [XmlElement("XMLCrtDt", Namespace = "urn:CBI:xsd:CBIHdrTrt.001.07")] 
    public DateTime XMLCrtDt { get; set; } 
    [XmlElement("RtrnAddrl", Namespace = "urn:CBI:xsd:CBIHdrTrt.001.07")] 
    public string RtrnAddrl { get; set; } 
} 
[Serializable] 
public class CBIHdrSrv 
{ 
    [XmlElement("SrvInfo",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")] 
    public SrvInfo SrvInfo { get; set; } 
    [XmlElement("Sender",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")] 
    public Sender Sender { get; set; } 
    [XmlElement("Receiver",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")] 
    public Receiver Receiver { get; set; } 
    [XmlElement("DiagInfo",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")] 
    public DiagInfo DiagInfo { get; set; } 
    [XmlElement("CongrInfo",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")] 
    public CongrInfo CongrInfo { get; set; } 
} 

[Serializable] 
public class SrvInfo 
{ 
    [XmlElement("SrvNm",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")] 
    public string SrvNm { get; set; } 
    [XmlElement("IdE2EMsg",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")] 
    public string IdE2EMsg { get; set; } 
    [XmlElement("XMLCrtDt",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")] 
    public DateTime XMLCrtDt { get; set; } 
} 

[Serializable] 
public class Sender 
{ 
    [XmlElement("IdCBISend",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")] 
    public string IdCBISend { get; set; } 
    [XmlElement("SendTyp",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")] 
    public string SendTyp { get; set; } 
    [XmlElement("CBIRefrSend",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")] 
    public string CBIRefrSend { get; set; } 
} 

[Serializable] 
public class Receiver 
{ 
    [XmlElement("IdCBIRecv",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")] 
    public string IdCBIRecv { get; set; } 
    [XmlElement("RecvTyp",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")] 
    public string RecvTyp { get; set; } 
    [XmlElement("CBIRefrRecv",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")] 
    public string CBIRefrRecv { get; set; } 
} 

[Serializable] 
public class DiagInfo 
{ 
    [XmlElement("UsrBnk",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")] 
    public string UsrBnk { get; set; } 
    [XmlElement("DiagVers",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")] 
    public string DiagVers { get; set; } 
    [XmlElement("ChkSbj",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")] 
    public string ChkSbj { get; set; } 
    [XmlElement("ChkDt",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")] 
    public DateTime ChkDt { get; set; } 
} 

[Serializable] 
public class CongrInfo 
{ 
    [XmlElement("SrvBdyNb",Namespace="urn:CBI:xsd:CBIHdrSrv.001.07")] 
    public int SrvBdyNb { get; set; } 
} 
[Serializable] 
public class CBIBdySDDStsRpt 
{ 
    [XmlElement("PhyMsgInf", Namespace = "urn:CBI:xsd:CBIBdySDDStsRpt.00.01.00")] 
    public PhyMsgInf PhyMsgInf { get; set; } 
    [XmlElement("CBIEnvelSDDStsRptLogMsg", Namespace = "urn:CBI:xsd:CBIBdySDDStsRpt.00.01.00")] 
    public CBIEnvelSDDStsRptLogMsg CBIEnvelSDDStsRptLogMsg { get; set; } 
} 

[Serializable] 
public class CBIEnvelSDDStsRptLogMsg 
{ 
    [XmlElement("CBISDDStsRptLogMsg", Namespace = "urn:CBI:xsd:CBIBdySDDStsRpt.00.01.00")] 
    public CBISDDStsRptLogMsg CBISDDStsRptLogMsg { get; set; } 
} 

[Serializable] 
public class CBISDDStsRptLogMsg 
{ 
    [XmlElement("GrpHdr", Namespace = "urn:CBI:xsd:CBISDDStsRptLogMsg.00.01.00")] 
    public GroupHeader GroupHeader { get; set; } 
    [XmlElement("OrgnlGrpInfAndSts", Namespace = "urn:CBI:xsd:CBISDDStsRptLogMsg.00.01.00")] 
    public OriginalGroupInformationAndStatus OriginalGroupInformationAndStatus { get; set; } 
    [XmlElement("OrgnlPmtInfAndSts", Namespace = "urn:CBI:xsd:CBISDDStsRptLogMsg.00.01.00")] 
    public List<OriginalPaymentInformationAndStatus> OriginalPaymentInformationAndStatus { get; set; } 
} 

我反序列化是很簡單:

   var sITA = new XmlSerializer(typeof(CBISDDStsRptPhyMsg)); 
       var xmlITA = new CBISDDStsRptPhyMsg(); 
       using(var reader = XmlReader.Create(fileInput.InputStream)) 
       { 
        xmlITA = (CBISDDStsRptPhyMsg)sITA.Deserialize(reader); 
       } 

,並在它的結束,我所有的3個主要對象爲null。

有沒有人有我在做什麼錯誤的線索?我已經設法序列化/反序列化我發現的每個SEPA文件(pain.00),但這個意大利標準在一段時間內給我帶來了一些頭痛。

有沒有人有能夠反序列化這個意大利標準的類? (不要打擾找到.xsd生成它,因爲我已經搜索了很多,並且.xsd無處可查!)

在此先感謝。

回答

1

在相同的命名空間的根的根元素的三個子元素,所述一個前綴RISPOSTASDD

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<RISPOSTASDD:CBISDDStsRptPhyMsg xmlns:HE2E="urn:CBI:xsd:CBIHdrSrv.001.07" xmlns:BODY="urn:CBI:xsd:CBIBdySDDStsRpt.00.01.00" xmlns:SGNT="urn:CBI:xsd:CBISgnInf.001.04" xmlns:RISPOSTASDD="urn:CBI:xsd:CBISDDStsRptPhyMsg.00.01.00" xmlns:LMSG="urn:CBI:xsd:CBISDDStsRptLogMsg.00.01.00" xmlns:HTRT="urn:CBI:xsd:CBIHdrTrt.001.07"> 
    <RISPOSTASDD:CBIHdrTrt> 
     <!-- Nested elements snipped --> 
    </RISPOSTASDD:CBIHdrTrt> 
    <RISPOSTASDD:CBIHdrSrv> 
     <!-- Nested elements snipped --> 
    </RISPOSTASDD:CBIHdrSrv> 
    <RISPOSTASDD:CBIBdySDDStsRpt> 
     <!-- Remaining XML not included in the question --> 

因此根類需要如下進行修改:

[Serializable, XmlRoot(Namespace = "urn:CBI:xsd:CBISDDStsRptPhyMsg.00.01.00")] 
public class CBISDDStsRptPhyMsg 
{ 
    [XmlElement("CBIHdrTrt", Namespace = "urn:CBI:xsd:CBISDDStsRptPhyMsg.00.01.00")] 
    public CBIHdrTrt CBIHdrTrt { get; set; } 
    [XmlElement("CBIHdrSrv", Namespace = "urn:CBI:xsd:CBISDDStsRptPhyMsg.00.01.00")] 
    public CBIHdrSrv CBIHdrSrv { get; set; } 
    [XmlElement("CBIBdySDDStsRpt", Namespace = "urn:CBI:xsd:CBISDDStsRptPhyMsg.00.01.00")] 
    public CBIBdySDDStsRpt CBIBdySDDStsRpt { get; set; } 
} 

(或者您可以忽略屬性上的Namespace =,因爲它與XmlRoot屬性中的相同。)

可能還有其他問題,但是您的問題不包含完整的mcve(XML和類都不完整),但最低限度這看起來不正確。

+0

謝謝你的幫助。現在它正確地反序列化。 –