Read Soap Message using C#

前端 未结 4 1216
傲寒
傲寒 2020-12-03 14:59

  
    

        
相关标签:
4条回答
  • 2020-12-03 15:18

    First you want to create a class to deseralize the xml values into

        public class bookHotelResponse {
          public int bookingReference { get; set; }
          public int bookingStatus { get; set; }
       } 
    

    Then you can utilize GetElementsByTagName to extract the body of the soap request and deseralize the request string into an object.

        private static T DeserializeInnerSoapObject<T>(string soapResponse)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(soapResponse);
    
            var soapBody = xmlDocument.GetElementsByTagName("soap:Body")[0];
            string innerObject = soapBody.InnerXml;
    
            XmlSerializer deserializer = new XmlSerializer(typeof(T));
    
            using (StringReader reader = new StringReader(innerObject))
            {
                return (T)deserializer.Deserialize(reader);
            }
        }
    
    0 讨论(0)
  • 2020-12-03 15:19

    Use LINQ2XML

    To read bookingStatus,do this

    XElement doc = XElement.Load("yourStream.xml");
    XNamespace s = "http://schemas.xmlsoap.org/soap/envelope/";//Envelop namespace s
    XNamespace bhr="urn:schemas-test:testgate:hotel:2012-06";//bookHotelResponse namespace
    XNamespace d="http://someURL";//d namespace
    
    foreach (var itm in doc.Descendants(s + "Body").Descendants(bhr+"bookHotelResponse"))
    {
    itm.Element(d+"bookingStatus").Value;//your bookingStatus value
    }
    

    LINQ2XML is cool though....:)

    0 讨论(0)
  • 2020-12-03 15:26

    BookHotelResponse is in the namespace urn:schemas-test:testgate:hotel:2012-06 (the default namespace in the sample xml) so you need to provide that namespace in your queries:

    XmlDocument document = new XmlDocument(); 
    document.LoadXml(soapmessage);  //loading soap message as string 
    XmlNamespaceManager manager = new XmlNamespaceManager(document.NameTable); 
    
    manager.AddNamespace("d", "http://someURL"); 
    manager.AddNamespace("bhr", "urn:schemas-test:testgate:hotel:2012-06"); 
    
    XmlNodeList xnList = document.SelectNodes("//bhr:bookHotelResponse", manager); 
    int nodes = xnList.Count; 
    
    foreach (XmlNode xn in xnList) 
    { 
        Status = xn["d:bookingStatus"].InnerText; 
    } 
    
    0 讨论(0)
  • 2020-12-03 15:28

    As I understand you want to get response from soap service. If so, you don't have to do all this hard work (making call, parsing xml, selecting nodes to get the response value) by yourself... instead you need to Add Service Reference to your project and it will do all the rest work for you, including generating class, making asmx call and so on... Read more about it here https://msdn.microsoft.com/en-us/library/bb628649.aspx

    Everything you'll need to do after adding reference is to invoke a class method something like this

    var latestRates = (new GateSoapClient())?.ExchangeRatesLatest();
    return latestRates?.Rates;
    
    0 讨论(0)
提交回复
热议问题