使用LINQ to XML来解析SOAP消息

Bil*_*Lee 5 c# soap linq-to-xml

我正在使用C#在Linq中加速XML并试图解析以下消息并且似乎没有取得多大进展.这是肥皂消息我不确定我是否需要使用命名空间.这是我想要格式化的SOAP消息.任何帮助将不胜感激.我试图提取值.谢谢.

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
 <soap:Body>
  <Lookup xmlns="http://ASR-RT/">
   <objIn>
    <transactionHeaderData>
     <intWebsiteId>1000</intWebsiteId>
     <strVendorData>test</strVendorData>
     <strVendorId>I07</strVendorId>
    </transactionHeaderData>
    <intCCN>17090769</intCCN>
    <strSurveyResponseFlag>Y</strSurveyResponseFlag>
   </objIn>
  </CCNLookup>
 </soap:Body>
</soap:Envelope>
Run Code Online (Sandbox Code Playgroud)

Cam*_*dan 9

如果这是与SOAP服务交互,请使用 添加服务引用wsdl.exe.

如果这只是解析XML,假设您已将SOAP响应放入名为soapDocument的XDocument中:

XNamespace ns = "http://ASR-RT/";
var objIns = 
    from objIn in soapDocument.Descendants(ns + "objIn")
    let header = objIn.Element(ns + "transactionHeaderData")
    select new
    {
        WebsiteId = (int) header.Element(ns + "intWebsiteId"),
        VendorData = header.Element(ns + "strVendorData").Value,
        VendorId = header.Element(ns + "strVendorId").Value,
        CCN = (int) objIn.Element(ns + "intCCN"),
        SurveyResponse = objIn.Element(ns + "strSurveyResponseFlag").Value,
    };
Run Code Online (Sandbox Code Playgroud)

这将为您提供一个IEnumerable的匿名类型,您将在该方法中作为完全强类型的对象处理.


dth*_*rpe 0

使用Linq的XDocument通过调用或类似的方式加载XML文本XDocument.Load()。然后,您可以使用以下函数遍历 xdoc 根的元素树

foreach (var x in xdoc.Elements("Lookup"))
{...}
Run Code Online (Sandbox Code Playgroud)