0

我正在尝试从 192.com 读取 XML 响应。

这是对客户详细信息的验证。

XML 响应设置如下(已删除客户详细信息):

<?xml version="1.0"?>
<Search Type="Result">
  <CountryCode>GBR</CountryCode>
  <Person>
    <Name>
      <Forename>XXXXX</Forename>
      <Surname>XXXXX</Surname>
    </Name>
    <DateOfBirth>XXXX-XX-XX</DateOfBirth>
    <Age>XX</Age>
  </Person>
  <Addresses>
    <Address Current="1">
      <Premise>X</Premise>
      <Postcode>XX99 9XX</Postcode>
      <CountryCode>GBR</CountryCode>
    </Address>
  </Addresses>
  <SearchOptions>
    <ProductCode>ProveID</ProductCode>
  </SearchOptions>
  <OurReference>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</OurReference>
  <SearchDate>2012-05-30T12:39:55</SearchDate>
  <Result>
   <Summary>
      <ReportSummary>
        <DatablocksSummary>
          <DatablockSummary>
            <Name>CreditReference</Name>
            <Decision>1</Decision>
          </DatablockSummary>
        </DatablocksSummary>
      </ReportSummary>
    </Summary>
    <CreditReference Type="Result">
      <Summary>
        <Decision>1</Decision>
        <DecisionReasons>
          <DecisionReason>
            <Element>CreditReferenceSummary/TotalNumberOfVerifications</Element>
            <Decision>1</Decision>
          </DecisionReason>
          <DecisionReason>
            <Element>CreditReferenceSummary/OpenAccountsMatch</Element>
            <Decision>1</Decision>
          </DecisionReason>
          <DecisionReason>
            <Element>CreditReferenceSummary/DateOfBirthMatch</Element>
            <Decision>1</Decision>
          </DecisionReason>
          <DecisionReason>
            <Element>CreditReferenceSummary/ElectoralRollMatch</Element>
            <Decision>-1</Decision>
          </DecisionReason>
          <DecisionReason>
            <Element>CreditReferenceSummary/ElectoralRollDateOfBirthMatch</Element>
            <Decision>-1</Decision>
          </DecisionReason>
          <DecisionReason>
            <Element>CreditReferenceSummary/TelephoneDirectoryMatch</Element>
            <Decision>1</Decision>
          </DecisionReason>
          <DecisionReason>
            <Element>CreditReferenceSummary/BOEMatch</Element>
            <Decision>-1</Decision>
          </DecisionReason>
          <DecisionReason>
            <Element>CreditReferenceSummary/PEPMatch</Element>
            <Decision>-1</Decision>
          </DecisionReason>
          <DecisionReason>
            <Element>CreditReferenceSummary/OFACMatch</Element>
            <Decision>-1</Decision>
          </DecisionReason>
          <DecisionReason>
            <Element>CreditReferenceSummary/DeceasedMatch</Element>
            <Decision>-1</Decision>
          </DecisionReason>
          <DecisionReason>
            <Element>CreditReferenceSummary/COAMatch</Element>
            <Decision>-1</Decision>
          </DecisionReason>
          <DecisionReason>
            <Element>CreditReferenceSummary/CIFASMatch</Element>
            <Decision>-1</Decision>
          </DecisionReason>
          <DecisionReason>
            <Element>CreditReferenceSummary/NoOfCCJ</Element>
            <Decision>-1</Decision>
          </DecisionReason>
          <DecisionReason>
            <Element>CreditReferenceSummary/NoOfOpenAccountsLenders</Element>
            <Decision>1</Decision>
          </DecisionReason>
        </DecisionReasons>
      </Summary>
      <CreditReferenceSummary>
          <CreditReferenceDecision/>
          <CreditReferenceScore/>
          <TotalNumberOfVerifications>7</TotalNumberOfVerifications>
          <OpenAccountsMatch>1</OpenAccountsMatch>
          <DateOfBirthMatch>1</DateOfBirthMatch>
          <ElectoralRollMatch>0</ElectoralRollMatch>
          <ElectoralRollDateOfBirthMatch>0</ElectoralRollDateOfBirthMatch>
          <TelephoneDirectoryMatch>1</TelephoneDirectoryMatch>
          <PhoneNumberMatch>0</PhoneNumberMatch>
          <DriverLicenceMatch/>
          <PassportMatch/>
          <DFATMatch/>
          <BOEMatch>0</BOEMatch>
          <PEPMatch>0</PEPMatch>
          <OFACMatch>0</OFACMatch>
          <DeceasedMatch>0</DeceasedMatch>
          <COAMatch>0</COAMatch>
          <CIFASMatch>0</CIFASMatch>
          <GoneAwayMatch/>
          <HighRiskAddressMatch/>
          <CommercialEntitiesAtAddressMatch/>
          <NoOfCommercialEntitiesAtAddress/>
          <NoOfCCJ>0</NoOfCCJ>
          <NoOfOpenAccountsLenders>5</NoOfOpenAccountsLenders>
          <IDVerified/>
      </CreditReferenceSummary>
      <CreditReferenceDetails>
        <StandardisedAddress Current="1">
          <SubPremise/>
          <Premise>999999</Premise>
          <SubStreet/>
          <Street>XXXXXXXXX</Street>
          <SubLocality/>
          <Locality>XXXXXXXXXXXXXXXXXXX</Locality>
          <PostTown>XXXXXXXXXXXXXXXXXXX</PostTown>
          <Region>XXXXXXXXXXXXXXX</Region>
          <Postcode>XX999XX</Postcode>
          <CountryCode>GBR</CountryCode>
        </StandardisedAddress>
      </CreditReferenceDetails>
    </CreditReference>
  </Result>
</Search>

我需要从此 XML 中提取以下元素<CreditReferenceSummary>

  • <TotalNumberOfVerifications>7</TotalNumberOfVerifications>
  • <OpenAccountsMatch>1</OpenAccountsMatch>
  • <DateOfBirthMatch>1</DateOfBirthMatch>
  • <DeceasedMatch>0</DeceasedMatch>
  • <NoOfOpenAccountsLenders>5</NoOfOpenAccountsLenders>

非常感谢任何帮助。先感谢您。

4

3 回答 3

1

我会使用 LINQ 来读取 XML

看一下

LINQ 读取 XML

于 2012-05-31T16:16:12.083 回答
1

假设根元素称为 root,因为您没有在示例中显示它,您可以使用 XPath 来读取所需元素的值:

XmlDocument doc = new XmlDocument();
doc.LoadXml(responseXml);
int totalNumberOfVerifications = int.Parse(doc.SelectSingleNode("Search/Result/CreditReference/CreditReferenceSummary/TotalNumberOfVerifications"));
// etc.

或者,您也可以使用 XML 反序列化来使用 XML 中的数据填充对象。我相信你可以通过四处搜索找到很多这样的例子:

http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

于 2012-05-31T16:47:53.840 回答
0

您可以使用此库和 XPath 来获取值:https ://github.com/ChuckSavage/XmlLib/

XElement root = XElement.Parse(string); // or .Load(file)
int TotalNumberOfVerifications = root.XGetElement("//TotalNumberOfVerifications", 0);
int OpenAccountsMatch = root.XGetElement("//OpenAccountsMatch", 0);
int DateOfBirthMatch = root.XGetElement("//DateOfBirthMatch", 0);
int DeceasedMatch = root.XGetElement("//DeceasedMatch", 0);
int NoOfOpenAccountsLenders = root.XGetElement("//NoOfOpenAccountsLenders", 0);

XGetElement 具有以下语法:

public T XGetElement<T>(xpath search string, default value to use if not found, param objects for search)
于 2012-06-05T10:43:30.223 回答