我有一个标准的SOAP消息,我从互联网上进行测试。
<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>
有没有办法将SOAP消息解析为通用列表或数据集或我可以枚举和使用的东西?我正在尝试创建一些基本上可以将SOAP消息反序列化为更通用的代码的代码,但我似乎能够找到的所有代码段都显示了您需要知道并提前对名称空间或父节点进行硬编码的示例。我真的希望我的代码能够动态地解析任何SOAP消息,并在可能的情况下创建节点的键值对。
答案 0 :(得分:1)
我不确定您期望什么样的通用列表,但我认为您可以使用Linq2Xml来解析您的SOAP。
例如,
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();