Link to home
Create AccountLog in
Avatar of ReverendProcess
ReverendProcess

asked on

How do you parse a Soap message with C# .NET LINQ?

Provided is a sample console application and Soap message.  I am attempting to parse the values from the Soap message using LINQ.  For some reason no values are found.  Any input would be appreciated. Thank you.


SOAP Message
<?xml version="1.0" encoding="UTF-8"?>
<Soap-Env:Envelope xmlns:Soap-Env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<Soap-Env:Body>
		<ns0:MyResponse xmlns:ns0="http://mywebservice.com/MyWebService">
			<MyResponse xsi:type="ns0:MyWebService_myResponse">
				<InventoryItem xsi:type="ns0:MyWebService_myResponseElement">
					<Product xsi:type="xsd:string">Plastic Spoons</Product>
					<StockNumber xsi:type="xsd:string">123456</StockNumber>
					<InventoryCount xsi:type="xsd:string">300</InventoryCount>
					<ProductDescription xsi:type="xsd:string">Plastic serving spoons.</ProductDescription>
				</InventoryItem>
			</MyResponse>
		</ns0:MyResponse>
	</Soap-Env:Body>
</Soap-Env:Envelope>

Open in new window



Sample Console App
using System;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;

namespace LinqQuestionApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Parsed Soap Values");
            Console.WriteLine("------------------");

            XDocument soapDocument = GetSoapDocument();

            ParsSoapDocument(soapDocument);

            // Exit application
            Console.WriteLine("");
            Console.WriteLine("Press any key to exit!");
            Console.ReadLine();
        }

        private static XDocument GetSoapDocument()
        {
            string filePath = @"C:\SoapDocument.xml";

            XDocument soapDocument = XDocument.Load(filePath);

            return soapDocument;
        }

        private static void ParsSoapDocument(XDocument soapDocument)
        {
            List<string> elementNamesList = new List<string>();
            elementNamesList.Add("Product");
            elementNamesList.Add("StockNumber");
            elementNamesList.Add("InventoryCount");
            elementNamesList.Add("ProductDescription");

            try
            {
                string elementNamespace = "ns0:MyWebService_myResponseElement";

                // Get the root element of the SOAP message.
                XElement rootElement = soapDocument.Root;

                // Get values from SOAP message.
                foreach (string elementName in elementNamesList)
                {
                    string messageNamespace = String.Format("{{{0}}}", elementNamespace);
                    string searchFor = String.Format("{0}{1}", messageNamespace, elementName);

                    // Select element based upon search value.
                    var selectedElements = from elements in rootElement.Descendants()
                                           where (elements.Name == searchFor)
                                           select elements;

                    foreach (XElement element in selectedElements)
                    {
                        string elementValue = element.Value;
                        string printValue = String.Format("{0} = {1}", elementName, elementValue);

                        Console.WriteLine(printValue);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("");
                Console.WriteLine("-----");
                Console.WriteLine("Error");
                Console.WriteLine("-----");
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

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