0

Estou precisando consumir um web service.

Adicionei o servidor no Add web reference, deu tudo certo. Na documentação ele pede o seguinte:

getPedido(login as string, passwd as string, XML as string) as String

Cadastra ou Recupera lote de pedidos de exames.

login: código do laboratório conveniado
passwd: senha do laboratório conveniado
XML: XML do tipo Registro.xsd ou ConsultaLote.xsd
Retorno: XML do tipo RegistroAdmitido.xsd

Na Chamada fiz o seguinte:

        string login = "6408";
        string senha = "2448";
        var xmlEnvio = File.ReadAllText(@"C:\Temp\HX120180228001.XML");
        string xml;
        string[] dados = null;
        XmlDocument xmlText = new XmlDocument();
        ws.XMLServer ws = new wsp.XMLServer();

        xml = ws.getPedido(login, senha,xmlEnvio);

Quando faço a chamada ele dar erro:

Informações adicionais: mensagem SOAP mal formada.

Acredito que a string xmlEnvio esteja errada. Como faço para colocar o xml como string no último campo?

010101 020202 28-02-2018 2 6408 1280220118001

2
  • É difícil chegar a um diagnóstico somente com essas informações, talvez o seu XML possua caracteres especiais ou aspas que estão quebrando a string. Debugando, você conferiu o xml gerado para a requisição? 19/03/2018 às 14:09
  • Parece que o serviço requer Protocolo SOAP para a comunicação entre as trocas de mensagens. Como está o xml que vc está enviando ? Informe o xml final para maiores informações...
    – Henrique
    20/03/2018 às 13:21

1 Resposta 1

0

Primeira coisa que você precisa fazer, é buscar os schemas dos XML de envio de retorno, ou seja, os arquivos Registro.xsd, ConsultaLote.xsd e RegistroAdmitido.xsd. Eles devem ter uma estrutura parecida com o xsd de exemplo abaixo.:

shiporder.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shiporder">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="orderperson" type="xs:string"/>
      <xs:element name="shipto">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="address" type="xs:string"/>
            <xs:element name="city" type="xs:string"/>
            <xs:element name="country" type="xs:string"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="item" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="title" type="xs:string"/>
            <xs:element name="note" type="xs:string" minOccurs="0"/>
            <xs:element name="quantity" type="xs:positiveInteger"/>
            <xs:element name="price" type="xs:decimal"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="orderid" type="xs:string" use="required"/>
  </xs:complexType>
</xs:element>
</xs:schema>

Então abra o seu Developer Command Propmt for VS... e gere as classes usando o xsd.exe. Lembrando que este comando deverá ser executado no mesmo diretório que está os seus esquemas.

xsd.exe shiporder.xsd /classes /namespace:ImportXsd

com isto, será gerado uma classe como a seguinte.:

namespace ImportXsd {
    using System.Xml.Serialization;


    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
    public partial class shiporder {

        private string orderpersonField;

        private shiporderShipto shiptoField;

        private shiporderItem[] itemField;

        private string orderidField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string orderperson {
            get {
                return this.orderpersonField;
            }
            set {
                this.orderpersonField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public shiporderShipto shipto {
            get {
                return this.shiptoField;
            }
            set {
                this.shiptoField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("item", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public shiporderItem[] item {
            get {
                return this.itemField;
            }
            set {
                this.itemField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string orderid {
            get {
                return this.orderidField;
            }
            set {
                this.orderidField = value;
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    public partial class shiporderShipto {

        private string nameField;

        private string addressField;

        private string cityField;

        private string countryField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string name {
            get {
                return this.nameField;
            }
            set {
                this.nameField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string address {
            get {
                return this.addressField;
            }
            set {
                this.addressField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string city {
            get {
                return this.cityField;
            }
            set {
                this.cityField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string country {
            get {
                return this.countryField;
            }
            set {
                this.countryField = value;
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    public partial class shiporderItem {

        private string titleField;

        private string noteField;

        private string quantityField;

        private decimal priceField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string title {
            get {
                return this.titleField;
            }
            set {
                this.titleField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string note {
            get {
                return this.noteField;
            }
            set {
                this.noteField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="positiveInteger")]
        public string quantity {
            get {
                return this.quantityField;
            }
            set {
                this.quantityField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public decimal price {
            get {
                return this.priceField;
            }
            set {
                this.priceField = value;
            }
        }
    }
}

abaixo segue um exemplo de como serializar e deserializar esta classe de e para um XML.:

var order = new ImportXsd.shiporder();
order.orderid = "ordemServico";
order.orderperson = "destinatario";
order.shipto = new ImportXsd.shiporderShipto
{
    address = "endereço",
    city = "cidade",
    country = "pais",
    name = "nome"
};
order.item = new ImportXsd.shiporderItem[] {
    new ImportXsd.shiporderItem {
        note = "nota 001",
        price = 100,
        quantity = "1",
        title = "titulo 001"
    },
    new ImportXsd.shiporderItem {
        note = "nota 002",
        price = 200,
        quantity = "2",
        title = "titulo 002"
    }
};

var xml = string.Empty;
var serialize = new XmlSerializer(typeof(ImportXsd.shiporder));
using (var strignWriter = new StringWriter())
{
    using (var xmlWriter = XmlWriter.Create(strignWriter, new XmlWriterSettings { Indent = true }))
    {
        serialize.Serialize(xmlWriter, order);
        xml = strignWriter.ToString();
    }
}

using (var strignReader = new StringReader(xml))
{
    using (var xmlReader = XmlReader.Create(strignReader))
    {
        order = serialize.Deserialize(xmlReader) as ImportXsd.shiporder;
    }
}

no exemplo acima, o XML gerado foi algo como.:

<?xml version="1.0" encoding="utf-16"?>
<shiporder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" orderid="ordemServico">
  <orderperson>destinatario</orderperson>
  <shipto>
    <name>nome</name>
    <address>endereço</address>
    <city>cidade</city>
    <country>pais</country>
  </shipto>
  <item>
    <title>titulo 001</title>
    <note>nota 001</note>
    <quantity>1</quantity>
    <price>100</price>
  </item>
  <item>
    <title>titulo 002</title>
    <note>nota 002</note>
    <quantity>2</quantity>
    <price>200</price>
  </item>
</shiporder>

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .