Solved

How do I parse SOAP

Posted on 2011-03-25
6
430 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

632 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