【发布时间】:2023-04-01 01:10:01
【问题描述】:
我是 .net 核心的新手,我正在尝试创建一个控制台应用程序,该应用程序在我的 .net 核心项目中使用 SOAP Web 服务。我一直在寻找图书馆,但找不到任何图书馆。我开始编码,并达到了能够从 SOAP 请求接收 XML 的程度。 我现在的问题是,我的代码以纯文本形式打印所有 XML,不是很有用。我试图在 XML 中访问我的数据,但没有成功。
我这样做是不是太难了?有没有一种方法或库可以使它更容易使用?
/// <summary>
/// Execute a Soap WebService call
/// </summary>
public static void Execute()
{
HttpWebRequest request = CreateWebRequest();
XmlDocument soapEnvelopeXml = new XmlDocument();
soapEnvelopeXml.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?>
<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:GET-AG=""http://ortsinfoV13x0.stromgas.services.getag.com"">
<soapenv:Header></soapenv:Header>
<soapenv:Body>
<GET-AG:getOrteByPlz>
<GET-AG:rec>
<GET-AG:auth_LoginName>----------</GET-AG:auth_LoginName>
<GET-AG:auth_Passwort>------</GET-AG:auth_Passwort>
</GET-AG:rec>
<GET-AG:plz>69126</GET-AG:plz>
</GET-AG:getOrteByPlz>
</soapenv:Body>
</soapenv:Envelope>"
);
using (Stream stream = request.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
using (WebResponse response = request.GetResponse())
{
using (StreamReader rd = new StreamReader(response.GetResponseStream()))
{
string soapResult = rd.ReadToEnd();
Console.WriteLine(soapResult);
}
}
}
/// <summary>
/// Create a soap webrequest to [Url]
/// </summary>
/// <returns></returns>
public static HttpWebRequest CreateWebRequest()
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"----------");
webRequest.Headers.Add(@"SOAP:Action");
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
}
static void Main(string[] args)
{
Execute();
}
这是 XML:
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns:getOrteByPlzResponse xmlns:ns="----------">
<ns:return xmlns:ax215="----------" xmlns:ax213="----------" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax213:Adressinfo">
<ax213:fehlerCode>1</ax213:fehlerCode>
<ax213:fehlerMeldungLength>0</ax213:fehlerMeldungLength>
<ax213:input xsi:type="ax213:AdressinfoParameter">
<ax213:strassenRequest xsi:nil="true"/>
<ax213:stringRequest>69126</ax213:stringRequest>
<ax213:validationsRequest xsi:nil="true"/>
</ax213:input>
<ax213:itag>BPTest1;;26.04.2019,15:16:21</ax213:itag>
<ax213:ort xsi:type="ax213:Ort">
<ax213:alort>22810500</ax213:alort>
<ax213:gkz>08221000</ax213:gkz>
<ax213:ortsname>Heidelberg</ax213:ortsname>
<ax213:ortsteil>Boxberg</ax213:ortsteil>
<ax213:ortsteil>Emmertsgrund</ax213:ortsteil>
<ax213:ortsteil>Rohrbach</ax213:ortsteil>
<ax213:ortsteil>Südstadt</ax213:ortsteil>
<ax213:ortsteilLength>4</ax213:ortsteilLength>
<ax213:plzLength>0</ax213:plzLength>
</ax213:ort>
<ax213:ortLength>1</ax213:ortLength>
<ax213:strasseLength>0</ax213:strasseLength>
<ax213:validation xsi:nil="true"/>
</ns:return>
</ns:getOrteByPlzResponse>
</soapenv:Body>
</soapenv:Envelope>
【问题讨论】:
标签: c# xml web-services asp.net-core soap