【问题标题】:C# Error: RestClient read value from SoapENV XMLC# 错误:RestClient 从 SoapENV XML 读取值
【发布时间】:2021-11-02 09:06:20
【问题描述】:

我尝试使用 RestClient 从 soapenv 获取会话 ID,但我得到的值为 0 或 -1。

这是我的代码

var client = new RestClient(url);
        client.Authenticator = new HttpBasicAuthenticator(name, pwd);
        var request = new RestRequest("/{sessionID}",Method.GET);

        var response = client.Get(request);
        var content = response.Content; // Raw content as string
        Console.WriteLine(response.ContentLength);

我的 Soapenv 看起来像这样

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v7="urn://oracle.bi.webservices/v7">
   <soapenv:Header/>
   <soapenv:Body>
      <v7:logon>
         <v7:name>pxxxx</v7:name>
         <v7:password>Pxxxx</v7:password>
      </v7:logon>
   </soapenv:Body>
</soapenv:Envelope>

我正在尝试获取看起来像这样的会话 ID (9insv92nke9lig83bqd5pu5tqt7kp8u0toe9l)

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:sawsoap="urn://oracle.bi.webservices/v7">
   <soap:Body>
      <sawsoap:logonResult>
         <sawsoap:sessionID xsi:type="xsd:string">9insv92nke9lig83bqd5pu5tqt7kp8u0toe9l</sawsoap:sessionID>
      </sawsoap:logonResult>
   </soap:Body>
</soap:Envelope>

无法弄清楚如何获取会话 ID。有人可以帮忙吗?

【问题讨论】:

  • 您的偏好是什么。您可以使用 XmlDocument 或 XDocument 或 XmlSerialization 或 Create a Controller 进行解析。
  • 无偏好。无论哪种方式都有效。

标签: c# xml rest-client soapexception


【解决方案1】:

尝试以下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApp1
{
    class Program
    {
        const string FILENAME = @"c:\Temp\Test.xml";
        static void Main(string[] args)
        {
            string content = File.ReadAllText(FILENAME);
            XDocument doc = XDocument.Parse(content);
            XElement xSessionID = doc.Descendants().Where(x => x.Name.LocalName == "sessionID").FirstOrDefault();
            string sessionID = (string)xSessionID;

        }
    }
}

【讨论】:

  • 我的 XML 在网络上,而不是本地文件。必须使用 HTTP GET 来获取它。我的错误是从网络获取文件时。
  • 您的响应有一个字符串内容,我的代码会解析该内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-25
  • 1970-01-01
  • 1970-01-01
  • 2016-12-22
  • 1970-01-01
  • 1970-01-01
  • 2011-06-28
相关资源
最近更新 更多