0

I am trying to read an XML response from 192.com.

This is a validation of customer details.

The XML response is set out as follows (Customer Details Removed):

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

I need to extract from this XML the elements for the following from the <CreditReferenceSummary>

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

Any help greatly appreciated. Thank you in advance.

Richard Gale
  • 1,816
  • 5
  • 28
  • 45

3 Answers3

1

I would use LINQ to read the XML

have a look at

LINQ to read XML

Community
  • 1
  • 1
Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
1

Assuming the root element is called root, since you didn't show it in your example, you could use XPath to read the value of the desired elements:

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

Or, alternatively, you could use XML deserialization to inflate an object with the data from the XML. I'm sure you could find many examples of that by searching around:

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

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
0

You can use this library and XPath to get the values: 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 has the syntax of:

public T XGetElement<T>(xpath search string, default value to use if not found, param objects for search)
Chuck Savage
  • 11,775
  • 6
  • 49
  • 69