Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I parse SOAP

Posted on 2011-03-25
6
Medium Priority
?
440 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

705 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