使用 LINQ to XML 解析 SOAP 消息

发布于 2024-10-05 03:42:15 字数 807 浏览 5 评论 0 原文

我正在加快 C# 中的 Linq to 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千年*琉璃梦 2024-10-12 03:42:15

如果这是关于与 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,
    };

这将为您提供一个 匿名类型 您在该方法中将其作为完全强类型对象进行处理。

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.

那支青花 2024-10-12 03:42:15

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

foreach (var x in xdoc.Elements("Lookup"))
{...}

Use Linq's XDocument to load the XML text by calling XDocument.Load() or similar. Then you can walk through the tree of elements off the xdoc's Root, using functions like

foreach (var x in xdoc.Elements("Lookup"))
{...}
风情万种。 2024-10-12 03:42:15

您可以将 XML 放入 XElement 中,然后只需执行以下操作:

rsp.Descendants("Lookup").ToList();

rsp.Descendants("objIn").ToList();

我认为这是最好的方法。我认为 XElement 是最好的选择。

You can get your XML into an XElement an then just do:

rsp.Descendants("Lookup").ToList();

Or

rsp.Descendants("objIn").ToList();

I think this is the best way to do it. I think that XElement is the best choice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文