You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VB.NET中读取SOAP XML响应并按CoverDesc提取Premium值

解决SOAP XML中按CoverDesc提取对应Premium值的问题

我来帮你搞定这个XML解析的问题!你用XmlDocument没得到预期结果,大概率是因为忽略了SOAP响应里的默认命名空间——你的XML里GetQuotResponse节点属于http://tempuri.org/这个命名空间,不处理的话XmlDocument根本找不到目标节点。

下面是完整的解决方案,我用C#代码给你演示:

// 假设你的SOAP响应XML字符串存在soapResponse变量里
string soapResponse = @"<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""> <soap:Body> <GetQuotResponse xmlns=""http://tempuri.org/""> <GetQuotResult> <POL_SYS_ID>30258992</POL_SYS_ID> <PROPOSAL_NO>10064/31/23/P/022669</PROPOSAL_NO> <VehicleIDV>29100</VehicleIDV> <ERROR_DESC>Proposal created successfully......!</ERROR_DESC> <ERROR_CODE>0</ERROR_CODE> <CoverDtlList> <CoverDtl> <CoverDesc>BASIC OD COVER</CoverDesc> <Premium>487.72</Premium> <Type /> </CoverDtl> <CoverDtl> <CoverDesc>OD TOTAL</CoverDesc> <Premium>487.72</Premium> <Type /> </CoverDtl> <CoverDtl> <CoverDesc>BASIC TP COVER</CoverDesc> <Premium>752</Premium> <Type /> </CoverDtl> <CoverDtl> <CoverDesc>GR36A-PA FOR OWNER DRIVER</CoverDesc> <Premium>315</Premium> <Type>ADD</Type> </CoverDtl> <CoverDtl> <CoverDesc>TP TOTAL</CoverDesc> <Premium>1067</Premium> <Type /> </CoverDtl> </CoverDtlList> </GetQuotResult> </GetQuotResponse> </soap:Body> </soap:Envelope>";

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(soapResponse);

// 创建命名空间管理器,处理默认命名空间
XmlNamespaceManager nsManager = new XmlNamespaceManager(xmlDoc.NameTable);
// 给默认命名空间加个前缀,比如"ns"
nsManager.AddNamespace("ns", "http://tempuri.org/");
// 还要加上soap的命名空间,因为要定位到soap:Body
nsManager.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");

// 定位到所有CoverDtl节点
XmlNodeList coverDtlNodes = xmlDoc.SelectNodes("//soap:Body/ns:GetQuotResponse/ns:GetQuotResult/ns:CoverDtlList/ns:CoverDtl", nsManager);

// 定义字典来存储CoverDesc和对应的Premium(如果有重复CoverDesc可以存列表)
Dictionary<string, List<decimal>> coverPremiumMap = new Dictionary<string, List<decimal>>();

foreach (XmlNode node in coverDtlNodes)
{
    string coverDesc = node.SelectSingleNode("ns:CoverDesc", nsManager)?.InnerText;
    string premiumStr = node.SelectSingleNode("ns:Premium", nsManager)?.InnerText;
    
    if (!string.IsNullOrEmpty(coverDesc) && decimal.TryParse(premiumStr, out decimal premium))
    {
        if (!coverPremiumMap.ContainsKey(coverDesc))
        {
            coverPremiumMap[coverDesc] = new List<decimal>();
        }
        coverPremiumMap[coverDesc].Add(premium);
    }
}

// 测试输出:比如获取"BASIC OD COVER"对应的Premium
if (coverPremiumMap.TryGetValue("BASIC OD COVER", out List<decimal> premiums))
{
    Console.WriteLine($"BASIC OD COVER的Premium值:{string.Join(", ", premiums)}");
}

关键要点说明:

  • 必须处理命名空间:XML里的GetQuotResponse及子节点都属于http://tempuri.org/,所以要用XmlNamespaceManager注册命名空间前缀,查询节点时带上前缀。
  • 遍历CoverDtl节点时,逐个提取CoverDesc和Premium,用字典存储可以方便后续按CoverDesc取值(如果有多个相同CoverDesc的情况,用列表存所有Premium)。
  • 注意类型转换:Premium是字符串,要转成decimal类型避免后续计算出错。

这样处理后,你就能准确提取到每个CoverDesc对应的所有Premium值了!

内容的提问来源于stack exchange,提问作者Huzefa Sayed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 18:02:41