从 Soap Envelope 请求中解析数据
Posted
技术标签:
【中文标题】从 Soap Envelope 请求中解析数据【英文标题】:Parse data from Soap Envelop request 【发布时间】:2021-02-15 09:49:15 【问题描述】:我有肥皂信封请求,我想从元素中提取一些数据。
下面是一个字符串格式的肥皂信封请求,我想提取位于“
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://ACORD.org/Standards/Life/445" xmlns:aaa="http://Life/PolicyDetails/3" xmlns:date="http://exslt.org/dates-and-times">
<soapenv:Header/>
<soapenv:Body>
<aaa:getPolicyDetailResponse>
<policyInfoResp>
<ns:TXLife>
<ns:TXLifeResponse PrimaryObjectID="Holding_1">
<ns:TransRefGUID>rsf4--41c3-8981-5a2c4518a7</ns:TransRefGUID>
<ns:TransType tc="201">Inforce Policy Inquiry</ns:TransType>
<ns:TransExeDate>2020-10-27-05:00</ns:TransExeDate>
<ns:TransExeTime>00:49:16-05:00</ns:TransExeTime>
<ns:TransMode tc="2">Original</ns:TransMode>
<ns:NoResponseOK tc="0">False</ns:NoResponseOK>
<ns:TestIndicator tc="0">False</ns:TestIndicator>
<ns:OLifEExtension VendorCode="AG" ExtensionCode="INI">
<![CDATA[
<![CDATA[[datafile]
Agent.CityState=ATLANTA, GA
Agent.Comp=AGL
Agent.Name=YRVFL KYCHBPO K
Agent.Number=000B078A
Agent.Status=Inactive
Agent.StrAddr=4286 DIGKGWZ DNVK KO DYG 202
]]]]>
<![CDATA[>]]>
</ns:OLifEExtension>
</ns:TXLifeResponse>
</ns:TXLife>
</policyInfoResp>
</aaa:getPolicyDetailResponse>
</soapenv:Body>
</soapenv:Envelope>
以下是我为解析数据而编写的代码,但出现空引用异常。
string contents = System.IO.File.ReadAllText(@"C:\Users\reg\CaReg-output1.txt");
var document = XDocument.Parse(contents);
string url = "http://ACORD.org/Standards/Life/2";
var ns = XNamespace.Get(url);
var xElements = document.Element(ns+"OLifEExtension").Value.ToString();
【问题讨论】:
【参考方案1】:尝试以下:
string contents = System.IO.File.ReadAllText(@"C:\Users\reg\CaReg-output1.txt");
XDocument document = XDocument.Parse(contents);
XElement root = document.Root;
XNamespace ns = root.GetNamespaceOfPrefix("ns");
List<string> OLifEExtension = document.Descendants(ns + "OLifEExtension").Select(x => (string)x).ToList();
【讨论】:
以上是关于从 Soap Envelope 请求中解析数据的主要内容,如果未能解决你的问题,请参考以下文章