2010-08-27 62 views
0

對於那些喜歡解決問題,這裏有一個大的:P問題具有複雜類型從對象轉換爲XML

嗯,我使用的Web服務開發一個系統,我在那裏發送和接收XML作爲參數(不是普通的參數,如Int,String,bool等)。

當我收到一個XML後,我用XSD驗證了XML,並將其轉換爲一個對象..在該過程之後,我還將該對象轉換爲XML(由XSD驗證)並返回爲我的請求的答案的WS。

嗯,我的問題:我有complexType,我需要使用反射轉換它,但是,我得到的問題,我從來沒有見過。

我的XSD是:

<xsd:element name="EnviarLoteRpsResposta"> 
    <xsd:complexType> 
     <xsd:choice> 
     <xsd:sequence> 
      <xsd:element name="NumeroLote" type="tsNumeroLote" minOccurs="1" maxOccurs="1"/> 
      <xsd:element name="DataRecebimento" type="xsd:dateTime" minOccurs="1" maxOccurs="1"/> 
      <xsd:element name="Protocolo" type="tsNumeroProtocolo" minOccurs="1" maxOccurs="1"/> 
     </xsd:sequence> 
     <xsd:element ref="ListaMensagemRetorno" minOccurs="1" maxOccurs="1"/> 
     </xsd:choice> 
    </xsd:complexType> 
    </xsd:element> 

我的類(GET和SET師範類):

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")] 
    [System.SerializableAttribute()] 
    [System.Diagnostics.DebuggerStepThroughAttribute()] 
    [System.ComponentModel.DesignerCategoryAttribute("code")] 
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.abrasf.org.br/ABRASF/arquivos/nfse.xsd")] 
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.abrasf.org.br/ABRASF/arquivos/nfse.xsd", IsNullable = false)] 
    public class EnviarLoteRpsResposta 
    { 

     private object[] itemsField; 

     private ItemsChoiceType[] itemsElementNameField; 

     /// <remarks/> 
     [System.Xml.Serialization.XmlElementAttribute("DataRecebimento", typeof(System.DateTime))] 
     [System.Xml.Serialization.XmlElementAttribute("ListaMensagemRetorno", typeof(ListaMensagemRetorno))] 
     [System.Xml.Serialization.XmlElementAttribute("NumeroLote", typeof(string), DataType = "nonNegativeInteger")] 
     [System.Xml.Serialization.XmlElementAttribute("Protocolo", typeof(string))] 
     [System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")] 
     public object[] Items 
     { 
      get 
      { 
       return this.itemsField; 
      } 
      set 
      { 
       this.itemsField = value; 
      } 
     } 

     /// <remarks/> 
     [System.Xml.Serialization.XmlElementAttribute("ItemsElementName")] 
     [System.Xml.Serialization.XmlIgnoreAttribute()] 
     public ItemsChoiceType[] ItemsElementName 
     { 
      get 
      { 
       return this.itemsElementNameField; 
      } 
      set 
      { 
       this.itemsElementNameField = value; 
      } 
     } 
    } 
} 

我如何使用這個類?:

EnviarLoteRpsResposta enviarLoteRpsResposta = new EnviarLoteRpsResposta(); 
enviarLoteRpsResposta.Items = new object[1]; 
       enviarLoteRpsResposta.Items[0] = DateTime.Now; 

       enviarLoteRpsResposta.ItemsElementName = new ItemsChoiceType[1]; 
       enviarLoteRpsResposta.ItemsElementName[0] = ItemsChoiceType.DataRecebimento; 

我嘗試將此對象轉換爲XML時發生錯誤: XmlSerializer xs = new XmlSerializer(enviarLoteRpsResposta.GetType());

我的錯誤:反映類型'NFSEWS.Models.Bean.EnviarLoteRpsResposta'的錯誤。

我不知道我該怎麼做可以解決這個..

+0

是有一些原因,你不要簡單地使用「添加服務引用「創建你的客戶?你爲什麼親手做這一切? – 2010-08-27 19:29:49

回答