简体   繁体   English

使用肥皂网络服务的.net核心控制台应用程序出现问题

[英]Issue with an console app with .net core which uses a soap web service

I'm new to .net core and I'm trying to create a console application which is using a SOAP web Service in my .net core project. 我是.net核心的新手,我正在尝试创建一个控制台应用程序,该控制台应用程序在.net核心项目中使用SOAP Web服务。 I have been looking for libraries but couldnt find any. 我一直在寻找图书馆,但找不到任何图书馆。 I started coding and got it to the Point where I am able to receive the XML from the SOAP request. 我开始编码,并将其传递到可以从SOAP请求接收XML的Point。 My problem now is, my Code, which prints all of the XML in plain text, isnt very usable. 现在我的问题是,我的代码以纯文本格式打印所有XML,不是非常有用。 Im trying to access my data within the XML, but without success. 我试图在XML中访问我的数据,但没有成功。

Am I doing this way too difficult? 我这样做太难了吗? Is there a way or library to use which makes it easier? 有没有一种方法或库可以使使用起来更容易?

/// <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();
        }


This is the XML: 这是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>

For easy and quick value access, you could use the XmlReader to read the SOAP response to find the LocalName / Value mappings from each XmlElement and then store the results in a NameValueCollection (or whatever ICollection you might prefer): 为了方便快捷地访问值,可以使用XmlReader读取SOAP响应,以从每个XmlElement查找LocalName / Value映射,然后将结果存储在NameValueCollection (或您可能喜欢的任何ICollection )中:

public static void Main()
{
    string strResponse = "<?xml version=\'1.0\' encoding=\'UTF-8\'?>\r\n<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n    <soapenv:Body>\r\n        <ns:getOrteByPlzResponse xmlns:ns=\"----------\">\r\n            <ns:return xmlns:ax215=\"----------\" xmlns:ax213=\"----------\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"ax213:Adressinfo\">\r\n                <ax213:fehlerCode>1</ax213:fehlerCode>\r\n                <ax213:fehlerMeldungLength>0</ax213:fehlerMeldungLength>\r\n                <ax213:input xsi:type=\"ax213:AdressinfoParameter\">\r\n                    <ax213:strassenRequest xsi:nil=\"true\"/>\r\n                    <ax213:stringRequest>69126</ax213:stringRequest>\r\n                    <ax213:validationsRequest xsi:nil=\"true\"/>\r\n                </ax213:input>\r\n                <ax213:itag>BPTest1;;26.04.2019,15:16:21</ax213:itag>\r\n                <ax213:ort xsi:type=\"ax213:Ort\">\r\n                    <ax213:alort>22810500</ax213:alort>\r\n                    <ax213:gkz>08221000</ax213:gkz>\r\n                    <ax213:ortsname>Heidelberg</ax213:ortsname>\r\n                    <ax213:ortsteil>Boxberg</ax213:ortsteil>\r\n                    <ax213:ortsteil>Emmertsgrund</ax213:ortsteil>\r\n                    <ax213:ortsteil>Rohrbach</ax213:ortsteil>\r\n                    <ax213:ortsteil>Südstadt</ax213:ortsteil>\r\n                    <ax213:ortsteilLength>4</ax213:ortsteilLength>\r\n                    <ax213:plzLength>0</ax213:plzLength>\r\n                </ax213:ort>\r\n                <ax213:ortLength>1</ax213:ortLength>\r\n                <ax213:strasseLength>0</ax213:strasseLength>\r\n                <ax213:validation xsi:nil=\"true\"/>\r\n            </ns:return>\r\n        </ns:getOrteByPlzResponse>\r\n    </soapenv:Body>\r\n</soapenv:Envelope>";
    NameValueCollection nameValueCollection = new NameValueCollection();
    try
    {
        using (StringReader stringReader = new StringReader(strResponse))
        {
            using (XmlReader xmlReader = XmlReader.Create(stringReader, new XmlReaderSettings()))
            {
                xmlReader.MoveToContent();

                string strKeyValue = null;
                do
                {
                    switch (xmlReader.NodeType)
                    {
                        case XmlNodeType.Text:
                            strKeyValue = xmlReader.Value;
                            break;

                        case XmlNodeType.EndElement:
                            if (!string.IsNullOrEmpty(strKeyValue))
                            {
                                nameValueCollection.Add(xmlReader.LocalName, strKeyValue);
                            }
                            break;

                        default:
                            strKeyValue = null;
                            break;
                    }
                } while (xmlReader.Read());
            }
        }
    }
    catch (Exception parentException)
    {
        Console.WriteLine(parentException);
    }
}

To obtain a string value for any XmlElement , you would need to access the NameValueCollection by the known key (without the namespace prefix). 要获取任何XmlElement的字符串值,您需要通过已知键(没有名称空间前缀)访问NameValueCollection For example: 例如:

nameValueCollection["fehlerCode"]

Sample output of AllKeys and values: 示例AllKeys和值的输出:

Console.Write(string.Join(Environment.NewLine, nameValueCollection.AllKeys.Select(x => x + ": " + nameValueCollection[x])));

fehlerCode: 1
fehlerMeldungLength: 0
stringRequest: 69126
itag: BPTest1;;26.04.2019,15:16:21
alort: 22810500
gkz: 08221000
ortsname: Heidelberg
ortsteil: Boxberg,Emmertsgrund,Rohrbach,Südstadt
ortsteilLength: 4
plzLength: 0
ortLength: 1
strasseLength: 0

Following code will print everything out nicely 以下代码可以很好地打印出所有内容

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication110
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement rtn = doc.Descendants().Where(x => x.Name.LocalName == "return").FirstOrDefault();
            string output = "";
            foreach (XElement child in rtn.Descendants())
            {
                string name = child.Name.LocalName;
                if (name.Contains("validation"))
                {
                    string nil = child.Attributes().First().Value;
                    output = name + " : " + nil;
                }
                else
                {
                    output = name + " : " + (string)child;
                }
                Console.WriteLine(output);
            }
            Console.ReadLine();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Unable to authenticate soap web service when adding via Microsoft WCF Web service Reference Provider to .NET Core 3 console application - Unable to authenticate soap web service when adding via Microsoft WCF Web service Reference Provider to .NET Core 3 console application ASP.NET核心web API项目中如何添加使用DBContecxt的后台服务 - How to add background service which uses DBContecxt in ASP.NET core web API project 连接到现有的 SOAP API 服务 in.Net Core 控制台应用程序 - Connect to existing SOAP API service in .Net Core console application 如何从 .NET Core 3.0 WPF 应用程序使用 SOAP Web 服务 - How to consume SOAP web service from .NET Core 3.0 WPF app 基于Soap的Web服务到.Net Core Service的转换 - Soap based web service to .Net Core Service conversion .NET Core 3.1 控制台应用作为 Windows 服务 - .NET Core 3.1 Console App as a Windows Service 将 .NET Core 控制台应用程序作为服务运行,还是重做? - Running .NET Core console app as a service, or redo? .Net core 2.0控制台应用程序作为Windows服务 - .Net core 2.0 console app as a windows service 网站引用了一个控制台应用程序,该应用程序使用NHibernate与数据库对话 - Web site references a console app which uses NHibernate to talk to database 在.NET Core中使用Java Web服务的问题 - Issue with consuming java web service in .net core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM