使用 LINQ 解析肥皂响应
我在解析以下肥皂响应时遇到问题。这是我第一次使用 LINQ,我发现的示例必须使用 XML 而不是 SOAP 信封。我如何获得不同“项目”的值。我知道有不同的选项(使用添加服务引用),但这不是我当前项目中的选项。
<SOAP-ENV:Envelope
xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"
xmlns:ns1=\"http://random.com/api/1/service\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
xmlns:ns2=\"http://xml.apache.org/xml-soap\"
xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"
SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">
<SOAP-ENV:Body>
<ns1:getBeatlesResponse>
<return xsi:type=\"ns2:Map\">
<item>
<key xsi:type=\"xsd:string\">error</key>
<value xsi:type=\"xsd:string\">OK</value>
</item>
<item>
<key xsi:type=\"xsd:string\">Beatles</key>
<value xsi:type=\"ns2:Map\">
<item>
<key xsi:type=\"xsd:int\">9</key>
<value xsi:type=\"xsd:string\">John Lennon</value>
</item>
<item>
<key xsi:type=\"xsd:int\">12</key>
<value xsi:type=\"xsd:string\">Paul McCartney</value>
</item>
<item>
<key xsi:type=\"xsd:int\">25</key>
<value xsi:type=\"xsd:string\">George Harrison</value>
</item>
<item>
<key xsi:type=\"xsd:int\">184</key>
<value xsi:type=\"xsd:string\">Ringo Starr</value>
</item>
</value>
</item>
</return>
</ns1:getBeatlesResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
提前致谢
I'm having trouble parsing the following soap response. This is my first time working with LINQ and must examples I've found use XML and not a SOAP envelope. How do I get the values of the different "items". I know there are different options (using add service reference) but it is not an option in my current project.
<SOAP-ENV:Envelope
xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"
xmlns:ns1=\"http://random.com/api/1/service\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
xmlns:ns2=\"http://xml.apache.org/xml-soap\"
xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"
SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">
<SOAP-ENV:Body>
<ns1:getBeatlesResponse>
<return xsi:type=\"ns2:Map\">
<item>
<key xsi:type=\"xsd:string\">error</key>
<value xsi:type=\"xsd:string\">OK</value>
</item>
<item>
<key xsi:type=\"xsd:string\">Beatles</key>
<value xsi:type=\"ns2:Map\">
<item>
<key xsi:type=\"xsd:int\">9</key>
<value xsi:type=\"xsd:string\">John Lennon</value>
</item>
<item>
<key xsi:type=\"xsd:int\">12</key>
<value xsi:type=\"xsd:string\">Paul McCartney</value>
</item>
<item>
<key xsi:type=\"xsd:int\">25</key>
<value xsi:type=\"xsd:string\">George Harrison</value>
</item>
<item>
<key xsi:type=\"xsd:int\">184</key>
<value xsi:type=\"xsd:string\">Ringo Starr</value>
</item>
</value>
</item>
</return>
</ns1:getBeatlesResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这一项非常棘手,因为您拥有的项目中可能也包含项目...因此,如果您执行类似的操作,
您将得到所有项目分离,并且其中一个项目具有所有值...
编辑:
这工作得很好,
您现在可以看到所有子项目及其正确的密钥...只有确定/错误会造成一些麻烦...但是您可以从最终列表中获取它们,并在它们不是任何关键时选择它们-值对...
希望这有帮助
this one is pretty tricky because you have items that have items which probably could have items too... so if you do something like this
you will get all the items separated and one which has all the values in one...
edit:
this works somewhat fine
you can now see all sub items with their correct key... only the ok/error is making some trouble... but you can get them from the final list and pick them if they are not any of the key-value pairs...
hope this helps
您应该能够通过将其加载到 中来解析它X文档。然后您可以通过指定元素名称来选择所需的值。最后,您可以将这些值作为新的匿名类型返回。可以在对此问题的回复中找到一个示例: 使用 LINQ到 XML 来解析 SOAP 消息
如果您有任何问题,请告诉我。
You should be able to parse this by loading it into an XDocument. Then you can select out the values you require by specifying the element names. Finally you could return these values as a new anonymous type. An example can be found in the response to this question: Using LINQ to XML to Parse a SOAP message
Let me know if you have any problems.