Solved

How do I parse SOAP

Posted on 2011-03-25
6
405 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

867 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

12 Experts available now in Live!

Get 1:1 Help Now