Solved

How do I parse SOAP

Posted on 2011-03-25
6
402 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now