使用LINQ to XML解析SOAP消息 [英] Using LINQ to XML to Parse a SOAP message

查看:370
本文介绍了使用LINQ to XML解析SOAP消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我起床的速度在LINQ to XML在C#中,并试图解析下面的消息,似乎没有要做出很大的进展。这里是SOAP消息,我不知道如果我也许我需要使用一个命名空间。这里是我试图格式SOAP消息。任何帮助将大大AP preciated。我试图提取值。谢谢你。

 <肥皂:信封的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance的xmlns:XSD =HTTP://www.w3 .ORG / 2001 / XML模式的xmlns:SOAP =http://schemas.xmlsoap.org/soap/envelope/>
 <肥皂:身体与GT;
  <查找的xmlns =HTTP:// ASR-RT />
   < objIn>
    < transactionHeaderData>
     &所述; intWebsiteId> 1000℃/ intWebsiteId>
     < strVendorData>测试与LT; / strVendorData>
     < strVendorId> I07< / strVendorId>
    < / transactionHeaderData>
    < intCCN> 17090769< / intCCN>
    < strSurveyResponseFlag> Y< / strSurveyResponseFlag>
   < / objIn>
  < / CCNLookup>
 < / SOAP:身体与GT;
< / SOAP:信封>


解决方案

如果这是关于一个SOAP服务,请使用交互
添加服务引用或的 Wsdl.exe用

如果这只是解析XML,假设你已经得到了SOAP响应到名为soapDocument一个XDocument:

 的XNamespace NS =HTTP:// ASR-RT /;
VAR objIns =
    从objIn在soapDocument.Descendants(NS +objIn)
    让头部= objIn.Element(NS +transactionHeaderData)
    新选择
    {
        WebsiteId =(INT)header.Element(NS +intWebsiteId),
        VendorData = header.Element(NS +strVendorData)。价值,
        VENDORID = header.Element(NS +strVendorId)。价值,
        CCN =(INT)objIn.Element(NS +intCCN),
        SurveyResponse = objIn.Element(NS +strSurveyResponseFlag)。价值,
    };

这会给你匿名类型你处理的完全强类型对象的IEnumerable内的方法。

I am getting up to speed in Linq to XML in C# and attempting to parse the following message and don't seem to be making much progress. Here is the soap message I am not sure if I perhaps I need to use a namespace. Here is the SOAP message I am trying to format. Any help would be greatly appreciated. I am attempting to extract the values. Thanks.

<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>

解决方案

If this is about interacting with a SOAP service please use Add Service Reference or wsdl.exe.

If this is just about parsing the XML, assuming you've got the SOAP response into an XDocument named soapDocument:

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,
    };

That will give you an IEnumerable of anonymous types you deal with as fully strongly typed objects within that method.

这篇关于使用LINQ to XML解析SOAP消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆