Link to home
Create AccountLog in
Avatar of Marthaj
MarthajFlag for United States of America

asked on

Forming a xml request using C#

 I am working in localhost, Windows 10 and IIS 10. I am trying to create a xml request using c#. 

This is what I have:

// CREATE THE XML REQUEST TO SEND 
      var xmlrequest = @"<?xml version="1.0" encoding="utf-8"?>"
          xmlrequest =   xmlrequest + "<soap:Envelope xmlns:xsi=" = @"http://www.w3.org/2001/XMLSchema-instance"
          xmlrequest =   xmlrequest + @xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
          xmlrequest =   xmlrequest + @xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/&gt;
          xmlrequest =   xmlrequest + "<soap:Body>"
          xmlrequest =   xmlrequest + "<BookExport xmlns=" + @"www.someprovider.com/webservices/+ " + ">"
           xmlrequest =   xmlrequest + "<BookingInformationXML>" + {txtbookid} + "</BookingInformationXML>"
                    xmlrequest =   xmlrequest + "</BookExport>"
                    xmlrequest =   xmlrequest + "</soap:Body>"
                    xmlrequest =   xmlrequest +  "</soap:Envelope>";
                
// GET STRING LENGTH OF XML REQUEST
       int stringlength = xmlrequest.length;            
                
 // ADD HEADERS    
   string headerinfo = "Content-type: text/xml; charset=utf-8";
          headerinfo = headerinfo + "Content-length: " + {stringlength};
           headerinfo = headerinfo + "SoapAction: " + string someUrl
 var string sndxmlrequest =  headerinfo + xmlrequest;

Open in new window

         

  Could someone please check my construction of it ?


I created the request as  xml document but not on how to send it as a request.

I have send xml request before but it's been years - ASP and PHP 5.6.

I also thought about using  HttpWebRequest and the HttpWebResponse  and attach the xml portion as a xml document but couldn't figure out how to that one.

I will be creating a WCF service for this coding - that I know how to do now.

I have looked and looked at the VS 2019 project options it provides and have no clue what to select. I think VS is too confusing for me right now.

(The last time I used vb, it was using objXMLHTTP = server.createobject("MSXML2.ServerHTTP")

And I used curl in php 5.6.)


Thank you for any help.



ASKER CERTIFIED SOLUTION
Avatar of Jonathan D.
Jonathan D.
Flag of Israel image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Marthaj

ASKER

Thank you for responding. I thought HTTPClient would be good to use. 

 I have am using Notepad++.

 Although I have VS 2019 developer installed, I find it confusing to use right now. 

I don't have the luxury of time right now to use it to my advantage - I am sure it would make things easier. Right now, I find it confusing as to what I would select. I did use it to create a wcf service. 


I know you've already selected the first comment as the solution, but this is truly the LONG way around.


I know Visual Studio seems daunting at first, but it works REALLY well with SOAP services, so let me walk you through the steps.


1. Launch Visual Studio 2019.


2. Click on Create a New Project (should be a button on the right side of the first popup)


3. Change the 3 dropdowns in the top-right to: C# -> Windows -> Desktop, and find "Windows Forms App (.NET Framework)" in the result:

User generated image

Just so you know, there are many options here that will work, but Windows Forms App (.NET Framework) is a good starting point.


Windows Forms App - When you want to build a Windows Forms application using newer .NET versions (.NET 5 and up).


Windows Forms App (.NET Framework) - When you want to build a Windows Forms application using .NET 4.


Since .NET 4 has been around forever and so has Windows Forms, there is a lot more code for that setup in the internet community. So just pick that one for now and click on Next. 


3. Name your project whatever you want. You can always rename it later, so don't worry too much about whatever it's called. Also, in the Framework dropdown, pick version 4.7.1 or higher.

User generated image


4. Click on Create and give Visual Studio a few seconds to set up your project.


5. Once the new project opens up, look in the top-right corner and you should see a Solution Explorer window. The 4th item down will be "References" - right-click on that and choose "Add Service Reference..." in the popup. 

User generated image


6. In the popup, just put in the URL to the WSDL, which is usually the web server endpoint with "?wsdl" at the very end, and then click on the Go button. If the WSDL is there, then it should list an entry in the "Services" box on the left, and then let you type in a namespace at the bottom. The namespace isn't terribly important - it can be whatever you want, but I'll keep the default "ServiceReference1" for these instructions and click on OK.

User generated image


7. After clicking on OK, you should be back to the main Visual Studio screen and Solution Explorer will now have a new "Connected Services" section with the service namespace you picked:

User generated image


8. Near the top of the Solution Explorer window, click on the icon that looks like 3 sheets of paper that aren't shuffled (usually the 4th icon from the right) - it's the "Show All Files" button. Once you click on that, you can expand the "ServiceReference1" and then expand the "Reference.svcmap" option and you should see a file in there called Reference.cs:

User generated image


That Reference.cs file is the code that Visual Studio has generated for you. It basically read the WSDL (which are the instructions for your SOAP service, saying, "request A has fields B and C, and they go in this order, etc..." and then used those instructions to build out a fully-functioning SOAP client for you. It takes care of all the XML generation for you - you don't have to worry about it at all. You literally just work with the classes it generated.


If you don't want to use Visual Studio, you can still take this Reference.cs file and bring it into your own Notepad++ project and use it.


9. Using the generated classes is usually just a matter of creating a new instance of the client (look for a class containing the word "PortClient" in the namespace, like ServiceReference1.MyServicePortClient), and then from there, you can just execute the SOAP actions straight from the client. For example, in my SOAP service, there's an "Update" API call I can run, so it looks like this:

User generated image


Again, the generated Reference.cs file is basically all the generated code for ServiceReference1 and the PortClient and all the API calls, so as long as you have that Reference.cs file loaded in your code, you can use the generated classes just like this.


You can literally have full SOAP client ready-to-use in like 2 minutes with this method. You don't have to worry about escaping characters, or character encoding, or if you need to add a new field but it's in the wrong order, and all the headaches that come with manually creating a SOAP request. And there is a TON of community support on how to tweak the client code (this is a WCF client, not a WCF server).

Avatar of Marthaj

ASKER

gr8gonzo  - WOW !! Great !!

Some questions - 

I do no show the project type - "Windows Form Apps (Net Framework).

 I selected  Asp.net Web Application(.Net Framework) as a project option.

 I have Net Framework 4.7.2 I selected Web Forms.

I tried the service reference on both url's and both times it gave me this:


Metadata contains a reference that cannot be resolved: 


User generated image

The request/response has to be contained within a WCF service so I would be passing only the selected bookid to the WCF service. The service is then suppose to do the Request/Response business and etc.


Do you know how I can test this in localhost mode?? 

I am not sure how  to change the value of the "<BookingInformationXML>"  element with this coding.

I have very good experience extracting/creating .etc. xml documents but it has been years since I have had to do a request/response.

 I had never created a service before last week, but I got a handle on doing that and have been successful. So much has changed.

Thank you, thank you. 





Avatar of Marthaj

ASKER

Jonathan D. remembered something - httpclient is meant to be used per application and not per user.

So, it will not work in this case since many users will be using the page - the page will call the web service and pass a parameter to it.

                    

                     

> I do no show the project type - "Windows Form Apps (Net Framework).  I selected  Asp.net Web Application(.Net Framework) as a project option.


If you're developing an ASP.NET page/site, then that's totally fine.



The error message you're seeing indicates the URL you're providing has an incorrect domain name.