0

I'm doing LoadXML, but I need to add a field from the form but I couldn't,

    XmlDocument soapEnvelopeDocument = new XmlDocument();
soapEnvelopeDocument.LoadXml(
    @"<?xml version=""1.0"" encoding=""utf-8""?>
    <soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
        <soap:Body>
            <Servis5001>
                <Kodu>abcdefg</Kodu>
                <Sifre>123456789</Sifre>
                <HesKodu>TXBHesKodu.Text</HesKodu>
            </Servis5001>
        </soap:Body>
    </soap:Envelope>");
 
return soapEnvelopeDocument;

I need to add the TXBHESKodu.Text from the form to the <HesKodu> field here. I think I couldn't make the upper quotation marks added into the file.

Can you show me how to do it?

2
  • 1
    It is better to use LINQ to XML. It is available in .Net Framework since 2007. Commented Dec 8, 2020 at 20:03
  • It looks like you want to use string interpolation to get the value of the text box into the string. By the way, building xml for SOAP is not something you usually want to do. The whole point of SOAP is to use a proxy builder so you can interact with it as objects.
    – Crowcoder
    Commented Dec 8, 2020 at 20:03

2 Answers 2

0

Here are two ways you could do that. The first being the simplest. Just use string formatting like this (note the $ sign before your @ sign):

XmlDocument soapEnvelopeDocument = new XmlDocument();
            soapEnvelopeDocument.LoadXml(
                $@"<?xml version=""1.0"" encoding=""utf-8""?>
                    <soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
                        <soap:Body>
                            <Servis5001>
                                <Kodu>abcdefg</Kodu>
                                <Sifre>123456789</Sifre>
                                <HesKodu>{TXBHesKodu.Text}</HesKodu>
                            </Servis5001>
                        </soap:Body>
                    </soap:Envelope>");

The second method uses the XML Dom to add the text into the element. We find the element using the XPath syntax:

var xmlDocument = GetXmlDocument(@"<?xml version=""1.0"" encoding=""utf-8""?>
                    <soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
                        <soap:Body>
                            <Servis5001>
                                <Kodu>abcdefg</Kodu>
                                <Sifre>123456789</Sifre>
                                <HesKodu></HesKodu>
                            </Servis5001>
                        </soap:Body>
                    </soap:Envelope>");

            XmlNode HesKodu = xmlDocument.SelectSingleNode("//Servis5001/HesKodu");

            HesKodu.InnerText = TXBHesKodu.Text;

You would be better off letting Visual Studio handle all the SOAP stuff for you. Take a look at adding a reference to a SOAP Web Service Reference (right click on project and Add => Web Service Reference). You can simply enter the url with ?wsdl at the end and it will generate everything you need to consume the web service.

You can use the url in a webbrowser too. If you have access to the service you can simply enter the url in the addressbar and press Enter. It should give you a description of the service and how to consume it.

2
  • Hi,Edited But It Didn't Work, I've Sent The Lines Of Code Can you review it? Commented Dec 9, 2020 at 8:24
  • Did you take a look at my recommendation at the bottom of my answer. It would make your life a lot easier. Commented Dec 12, 2020 at 19:01
0

Method Usage Error When I Want to Add TXT HesKodu.Text

private static XmlDocument CreateSoapEnvelope()
        {
            XmlDocument soapEnvelopeDocument = new XmlDocument();
            soapEnvelopeDocument.LoadXml(
                $@"<?xml version=""1.0"" encoding=""utf-8""?>
                    <soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
                        <soap:Body>
                            <Servis5001>
                                <Kodu>abcdef</Kodu>
                                <Sifre>abcdef</Sifre>
                                <HesKodu>{TXBHesKodu.Text}</HesKodu>
                            </Servis5001>
                        </soap:Body>
                    </soap:Envelope>");

            return soapEnvelopeDocument;
        }

SOAP Whatever I Use My Blog Is All The Code Is As Below, Maybe If You Want To See It

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace TOBBHesKoduSorgulama
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void BTNSorgula_Click(object sender, EventArgs e)
        {
            var _url = "https://kpsoda.tobb.org.tr/hesservis.php?wsdl";
            var _action = "https://kpsoda.tobb.org.tr/hesservis.php?op=Servis5001";
            var result = "";
            XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
            HttpWebRequest webRequest = CreateWebRequest(_url, _action);
            InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

            // begin async call to web request.
            IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);

            // suspend this thread until call is complete. You might want to
            // do something usefull here like update your UI.
            asyncResult.AsyncWaitHandle.WaitOne();

            // get the response from the completed web request.
            string soapResult;
            using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
            {
                using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
                {

                    soapResult = rd.ReadToEnd();
                }
                result = soapResult;
                Console.Write(soapResult);
            }

        }

        private static HttpWebRequest CreateWebRequest(string url, string action)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Headers.Add("SOAPAction", action);
            webRequest.ContentType = "text/xml;charset=\"utf-8\"";
            webRequest.Accept = "text/xml";
            webRequest.Method = "POST";
            return webRequest;
        }

        private static XmlDocument CreateSoapEnvelope()
        {
            XmlDocument soapEnvelopeDocument = new XmlDocument();
            soapEnvelopeDocument.LoadXml(
                $@"<?xml version=""1.0"" encoding=""utf-8""?>
                    <soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
                        <soap:Body>
                            <Servis5001>
                                <Kodu>abcdef</Kodu>
                                <Sifre>abcdef</Sifre>
                                <HesKodu>{TXBHesKodu.Text}</HesKodu>
                            </Servis5001>
                        </soap:Body>
                    </soap:Envelope>");

            return soapEnvelopeDocument;
        }

        private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
        {
            using (Stream stream = webRequest.GetRequestStream())
            {
                soapEnvelopeXml.Save(stream);
            }
        }
    }
}

I also tried the Service References Process But I Didn't Know How To Request

string Kodu = "Kodu";
string Sifre = "Sifre";
string HesKodu = "HesKodu";

TOBBHesKoduSorgulamaServices.Servis5001Request S5001R = new TOBBHesKoduSorgulamaServices.Servis5001Request();
S5001R.Kodu = Kodu;
S5001R.Sifre = Sifre;
S5001R.HesKodu = HesKodu;

TOBBHesKoduSorgulamaServices.Servis5001Response S5001Response = ?

Which One Is Right For Me? Waiting for your support, good work ...

1
  • You are able to edit your question. You should not add an answer to provide more information.
    – Crowcoder
    Commented Dec 9, 2020 at 13:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.