Solved

How do I parse SOAP

Posted on 2011-03-25
6
417 Views
Last Modified: 2012-08-13
I have a soap object, similiar to the one attached here; and I need to load it in c# and extract the values for each element...

What's the best way to do this?
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
	<soapenv:Body>
	<ns:getPublisherResponse xmlns:ns="http://zapi.zedo.com">
	<ns:return xmlns:ax25="http://soap.reportengine.zedo.com/xsd" xmlns:ax22="http://util.zedo.com/xsd" xmlns:ax24="http://client.api.zedo.com/xsd" xmlns:ax21="http://base.zedo.com/xsd" type="com.zedo.api.client.Publisher">
	<ax24:billingContact type="com.zedo.api.client.Contact">
			<ax24:address1></ax24:address1>
			<ax24:address2></ax24:address2>
			<ax24:city></ax24:city>
			<ax24:country></ax24:country>
			<ax24:email></ax24:email>
			<ax24:fax></ax24:fax>
			<ax24:firstName></ax24:firstName>
			<ax24:lastName></ax24:lastName>
			<ax24:phone></ax24:phone>
			<ax24:state></ax24:state>
			<ax24:zipCode></ax24:zipCode>
	</ax24:billingContact>
	<ax24:code>24965</ax24:code>
	<ax24:id>543025</ax24:id>
	<ax24:name>CR3823 sample.com</ax24:name>
	<ax24:salesContact type="com.zedo.api.client.Contact">
			<ax24:address1></ax24:address1>
			<ax24:address2></ax24:address2>
			<ax24:city></ax24:city>
			<ax24:country></ax24:country>
			<ax24:email></ax24:email>
			<ax24:fax></ax24:fax>
			<ax24:firstName></ax24:firstName>
			<ax24:lastName></ax24:lastName>
			<ax24:phone></ax24:phone>
			<ax24:state></ax24:state>
			<ax24:zipCode></ax24:zipCode>
	</ax24:salesContact>
	<ax24:siteLocation>http://soinappropriate.com</ax24:siteLocation>
	<ax24:status>A</ax24:status>
	</ns:return>
	</ns:getPublisherResponse>
	</soapenv:Body>
</soapenv:Envelope>

Open in new window

0
Comment
Question by:adworldmedia
  • 4
  • 2
6 Comments
 
LVL 5

Expert Comment

by:MikkelAStrojek
ID: 35218007
You can try something like this.
 
 private static void ReadSoap(string filename)
        {
            System.Xml.Linq.XDocument d =System.Xml.Linq.XDocument.Load (filename);
            var selectedElements = d.Descendants();

            foreach (System.Xml.Linq.XElement element in selectedElements)
            {
                string printValue = String.Format("Name: {0} = Value: {1}", element.Name.LocalName  ,  element.Value);
                Console.WriteLine(printValue);
            }
        }

Open in new window

0
 

Author Comment

by:adworldmedia
ID: 35218553
Thanks, but what if I wanted to extract each specific value, such as "address1" or "firstname" ?
0
 
LVL 5

Expert Comment

by:MikkelAStrojek
ID: 35218638
You could use linq directly still using parts of code already posted
var v = from System.Xml.Linq.XElement element in selectedElements where element.Name.LocalName.Equals("firstName") select element;

            foreach (System.Xml.Linq.XElement element in v)
            {
                string printValue = String.Format("Name: {0} = Value: {1}", element.Name.LocalName, element.Value);
                Console.WriteLine(printValue);
            }

Open in new window

0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 5

Expert Comment

by:MikkelAStrojek
ID: 35218669
But you would probably want to look at the "Descendants" of each contact(Your example soap) so you don't mix them up.
0
 

Author Comment

by:adworldmedia
ID: 35218785
Thanks, everything is looking awesome.. just one more item.  I know the results will always be one result...  how can I get that value without doing the foreach loop?

like a v[0].value ?

-Joe
0
 
LVL 5

Accepted Solution

by:
MikkelAStrojek earned 500 total points
ID: 35218869
Example code. Note if you encounter more than one element an exception is thrown by the Single method
var v = (from System.Xml.Linq.XElement element in selectedElements where element.Name.LocalName.Equals("billingContact") select element).Single();
            var myDescendantElements = v.Descendants();

            var fn = (from System.Xml.Linq.XElement element in myDescendantElements where element.Name.LocalName.Equals("firstName") select element).Single();

            string printValue = String.Format("Name: {0} = Value: {1}", fn.Name.LocalName, fn.Value);
            Console.WriteLine(printValue);

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSIS Feed MS SQL Server Stored procedure with loop 4 38
Generate Unique ID in VB.NET 21 69
Time comparison asp 1 16
TFS 2015 Access denied. 1 16
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question