Reputation: 99
I have a standard SOAP message that I got off the internet for testing.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
<ns1:RequestHeader soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0" xmlns:ns1="https://www.google.com/apis/ads/publisher/v201502">
<ns1:networkCode>123456</ns1:networkCode>
<ns1:applicationName>DfpApi-Java-2.1.0-dfp_test</ns1:applicationName>
</ns1:RequestHeader>
</soapenv:Header>
<soapenv:Body>
<getAdUnitsByStatement xmlns="https://www.google.com/apis/ads/publisher/v201502">
<filterStatement>
<query>WHERE parentId IS NULL LIMIT 500</query>
</filterStatement>
</getAdUnitsByStatement>
</soapenv:Body>
</soapenv:Envelope>
Is there a way to parse a SOAP message into a generic list or dataset or something that I can enumerate and work with? I'm trying to create some code that essentially can deserialize a SOAP message into something more generic, but all the snippets I've seemed to be able to find show examples where you need to know and hard code the namespaces or parent nodes in advance. I'd really like my code to be able to dynamically parse essentially ANY SOAP message and create key value pairs of the nodes down the road if at all possible.
Upvotes: 0
Views: 1254
Reputation: 15354
I am not sure what kind of generic list you expect, but I think you can use Linq2Xml to parse your SOAP.
For ex.,
var xDoc = XDocument.Parse(xmlstr);
var list = xDoc.Descendants()
.Select(x=>new{
Name = x.Name.LocalName,
Namespace = x.Name.Namespace.ToString(),
Attributes = x.Attributes().ToDictionary(a=>a.Name.LocalName, a=>a.Value),
Value = x.HasElements ? "" : x.Value
})
.ToList();
Upvotes: 1