使用SSO/SOAP响应

本文关键字:响应 SOAP SSO 使用 | 更新日期: 2023-09-27 18:05:18

我有这个SSO/SOAP WebService,我需要消费一些信息,我不使用c#/ASP。. NET,所以我不知道如何得到一个可用对象的响应。

这是SOAP

的返回值
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <RetornaEstadosResponse xmlns="http://WebService-MultiLogin-2013/">
      <RetornaEstadosResult>
        <EstadosMDL>
          <ID>int</ID>
          <Nome>string</Nome>
          <Sigla>string</Sigla>
        </EstadosMDL>
        <EstadosMDL>
          <ID>int</ID>
          <Nome>string</Nome>
          <Sigla>string</Sigla>
        </EstadosMDL>
      </RetornaEstadosResult>
    </RetornaEstadosResponse>
  </soap12:Body>
</soap12:Envelope>

我有一个我想用返回值填充的对象:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Collections;
namespace Library
{
    public class Estados
    {
        //Here i'm creating the object
        private AdminMaster.RetornaEstadosPorMarca.Estados ssoEstados = new AdminMaster.RetornaEstadosPorMarca.Estados();
        public List<Estados> lstEstado = new List<Estados>();
        #region Propriedades
            public int ID
            {
                get;
                set;
            }
            public string Nome
            {
                get;
                set;
            }
            public string Sigla
            {
                get;
                set;
            }
        #endregion
        #region Métodos
            /// <summary>
            /// Lista Todos os Estados
            /// </summary>
            /// <returns></returns>
            public void Listar(Library.Estados objEstados)
            {
                //Here i'm calling the function that will return me the States(response)
                ssoEstados.RetornaEstadosPorMarca(Library.Configuracoes.ChaveSSO, Library.Configuracoes.Marca);
            }
        #endregion
    }
}

现在,我如何读取/消费响应并将其放在我的Estados对象中以在项目上使用?

编辑

我试过了:

StringBuilder output = new StringBuilder();
// Create an XmlReader
using (XmlReader reader = XmlReader.Create(new StringReader(respSSO)))
{
    reader.ReadToFollowing("EstadosMDL");
    reader.MoveToFirstAttribute();
    string genre = reader.Value;
    return genre;
 }

并得到这个错误:

[NullReferenceException: Object reference not set to an instance of an object.]
   Library.Configuracoes.get_ChaveSSO() +95
   Library.Estados.Listar() +89
   AdminMaster.SiteMaster.ListaEstados() +113
   AdminMaster.SiteMaster.Page_Load(Object sender, EventArgs e) +50
   System.Web.UI.Control.LoadRecursive() +70
   System.Web.UI.Control.LoadRecursive() +189
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3177

使用SSO/SOAP响应

一般来说,您从SOAP请求返回的响应将是XML格式的字符串。然后使用XmlReader将字符串解析为XML元素,并在解析时处理它们。

XmlReader: msdn.microsoft.com/en-us/library/cc189056(v=vs.95).aspx

这个例子展示了如何通过一个开关解析每个节点:

// Parse the file and display each of the nodes.
        while (reader.Read())
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Element:
                    writer.WriteStartElement(reader.Name);
                    break;
                case XmlNodeType.Text:
                    writer.WriteString(reader.Value);
                    break;
                case XmlNodeType.XmlDeclaration:
                case XmlNodeType.ProcessingInstruction:
                    writer.WriteProcessingInstruction(reader.Name, reader.Value);
                    break;
                case XmlNodeType.Comment:
                    writer.WriteComment(reader.Value);
                    break;
                case XmlNodeType.EndElement:
                    writer.WriteFullEndElement();
                    break;
            }
        }