Asked by:
Consume SOAP and sending response back as Rest in C#

Question
-
I am working with .net 4.5 C# project. I am consuming SOAP service and then response back them to with REST with SharePoint 2013.
But the issue is part of SOAP response is not parsing as XML but as string. I don't know what I am doing wrong.
Screen marked part parsing as string not XMLCode for Consuming SOAP
public HttpWebRequest CreateWebRequest(string URL) { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"" + URL); webRequest.Headers.Add(@"SOAP:Action"); webRequest.ContentType = "text/xml;charset=\"utf-8\""; webRequest.Accept = "text/xml"; webRequest.Method = "POST"; return webRequest; } public string ConsumeSOAP(string XML, string URL) { HttpWebRequest request = CreateWebRequest(URL); XmlDocument soapEnvelopeXml = new XmlDocument(); soapEnvelopeXml.LoadXml(@"" + XML); using (Stream stream = request.GetRequestStream()) { soapEnvelopeXml.Save(stream); } using (WebResponse response = request.GetResponse()) { using (StreamReader rd = new StreamReader(response.GetResponseStream())) { string soapResult = rd.ReadToEnd(); return soapResult; } } }
Code for SOAP response to REST
public string doLogin(string memberId, string password) { string XML = null; try { SOAPService soap = new SOAPService(); XNamespace ns = @"http://schemas.xmlsoap.org/soap/envelope/"; string URL = URL; string User = User; string Password = Password; string authenticateXML = string.Format(@"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:web=""http://webservices.xxx.xxx.com/""> <soapenv:Header> <web:header> <passWord>{0}</passWord> <userName>{1}</userName> </web:header> </soapenv:Header> <soapenv:Body> <web:authenticate> <authenticateRequest> <password>{2}</password> <username>{3}</username> </authenticateRequest> </web:authenticate> </soapenv:Body> </soapenv:Envelope>", Password, User, password, memberId); string authenticateResponse = soap.ConsumeSOAP(authenticateXML, URL); var authenticateResponseValue = XDocument.Parse(authenticateResponse); XML = authenticateResponseValue.Descendants((XNamespace)"http://schemas.xmlsoap.org/soap/envelope/" + "Body").First().FirstNode.ToString(); } catch { } return XML; }
Interface for the REST
[OperationContract] [WebGet(ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "/doLogin?memberId={memberId}&password={password}")] string doLogin(string memberId, string password);
[1]: https://i.stack.imgur.com/na54O.pngFriday, April 28, 2017 5:56 AM
All replies
-
Hi,
The following code for your reference:
using System.Xml; namespace REST_XML_LIST_GET { class Program { static XmlNamespaceManager xmlnspm = new XmlNamespaceManager(new NameTable()); static Uri sharepointUrl = new Uri("Site URL/"); static void Main(string[] args) { xmlnspm.AddNamespace("atom", "http://www.w3.org/2005/Atom"); xmlnspm.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices"); xmlnspm.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"); NetworkCredential cred = new System.Net.NetworkCredential("username", password", "domain"); HttpWebRequest listRequest = (HttpWebRequest)HttpWebRequest.Create(sharepointUrl.ToString() + "_api/Web/lists/getByTitle('List Name')/items"); listRequest.Method = "GET"; listRequest.Accept = "application/atom+xml"; listRequest.ContentType = "application/atom+xml;type=entry"; listRequest.Credentials = cred; HttpWebResponse listResponse = (HttpWebResponse)listRequest.GetResponse(); StreamReader listReader = new StreamReader(listResponse.GetResponseStream()); var listXml = new XmlDocument(); listXml.LoadXml(listReader.ReadToEnd()); //Method 1 Seperate node list var titleList = listXml.SelectNodes("//atom:entry/atom:content/m:properties/d:Title", xmlnspm); var idList = listXml.SelectNodes("//atom:entry/atom:content/m:properties/d:ID", xmlnspm); int i = 0; foreach (XmlNode title in titleList) { Console.WriteLine(title.InnerXml+" "+idList[i++].InnerXml); } //Method 2 single node list var prop = listXml.SelectNodes("//atom:entry/atom:content/m:properties", xmlnspm); foreach (XmlNode ndlist in prop) { Console.WriteLine(ndlist.SelectSingleNode("d:Title", xmlnspm).InnerXml + " " + ndlist.SelectSingleNode("d:ID", xmlnspm).InnerXml); } Console.ReadLine(); } } }
More information:
Working With SharePoint 2013 REST API in a C# Managed Code
Sharepoint 2013 REST API: The C# Connection: Part 1 Using System.Net.Http.HttpClient
Step By Step Calling SharePoint 2013 REST API From C#.
http://www.sp4geeks.com/2015/04/20/step-by-step-calling-sharepoint-2013-rest-api-from-c/
If you want to access SharePoint content in client side using C#, we can also use .NET client object Model to achieve it.
Complete basic operations using SharePoint 2013 client library code
https://msdn.microsoft.com/en-us/library/office/fp179912.aspx
Best Regards,
Please remember to mark the replies as answers if they help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com- Proposed as answer by Lee__liMicrosoft contingent staff Thursday, May 4, 2017 8:54 AM
Monday, May 1, 2017 3:14 AM