【问题标题】:Problem with a conversion of a complexType FROM object TO XML复杂类型从对象到 XML 的转换问题
【发布时间】:2010-08-27 14:22:01
【问题描述】:

对于那些喜欢解决问题的人来说,这是一个大问题:P

嗯,我正在开发一个使用 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”的错误。

我不知道我能做些什么来解决这个问题..

【问题讨论】:

  • 您是否有某些原因不简单地使用“添加服务引用”来创建您的客户端?为什么要手动完成所有这些操作?

标签: xml web-services reflection xml-serialization


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多