Web service with c#

Asked

Viewed 164 times

0

I need to consume a web service.

I added the server to Add web reference, everything worked out. In the documentation he asks for the following:

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

Cadastra or Recovers batch of examination requests.

login: agreed laboratory code
passwd: agreed laboratory password
XML: XML type Record.xsd or Query.xsd
Retorno: XML of the Registered type.xsd

In the Call I did the following:

        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);

When I make the call he makes a mistake:

Informações adicionais: mensagem SOAP mal formada.

I believe the string xmlEnvio is wrong. How do I put the xml as string in the last field?

010101 020202 28-02-2018 2 6408 1280220118001

  • It is difficult to get a diagnosis only with this information, maybe your XML has special characters or quotes that are breaking the string. Debugging, you checked the generated xml for the request?

  • It seems that the service requires SOAP Protocol for the communication between messages exchanges. How is the xml you are sending ? Enter the final xml for more information...

1 answer

0


First thing you need to do is get the schemas dos XML return sending, ie the files Registro.xsd, ConsultaLote.xsd and RegistroAdmitido.xsd. They must have a structure similar to the xsd example below.:

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>

So open your Developer Command Propmt for VS... and manage the classes using the xsd.exe. Remembering that this command should be executed in the same directory as your schematics.

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

with this, a class such as the following will be generated.:

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;
            }
        }
    }
}

below follows an example of how serializar and deserializar this class of and for a 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;
    }
}

in the above example, the generated XML was something like.:

<?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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.