Not getting desired elements from XDocument

44 views Asked by At

I am doing a company name search from a website. The website returns the response as XML. I am trying to convert the XML to a custom List. I have converted the XML response into a XDocument but the node values that I am trying to get are not retrieved. I have not done this before so I am not sure how to do this. Below is the XML response,

<?xml version="1.0" encoding="UTF-8"?>
<GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope"     xmlns:dsig="http://www.w3.org/2000/09/xmldsig#"     xmlns:gt="http://www.govtalk.gov.uk/schemas/govtalk/core"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.govtalk.gov.uk/CM/envelope http://xmlgw.companieshouse.gov.uk/v1-0/schema/Egov_ch-v2-0.xsd">
<EnvelopeVersion>1.0</EnvelopeVersion>
<Header>
  <MessageDetails>
     <Class>NameSearch</Class>
     <Qualifier>response</Qualifier>
     <TransactionID>Transaction ID</TransactionID>
     <GatewayTimestamp>Time</GatewayTimestamp>
  </MessageDetails>
  <SenderDetails>
     <IDAuthentication>
        <SenderID>SenderID</SenderID>
        <Authentication>
           <Method>CHMD5</Method>
           <Value />
        </Authentication>
     </IDAuthentication>
  </SenderDetails>
</Header>
<GovTalkDetails>
   <Keys />
</GovTalkDetails>
<Body>
  <NameSearch xmlns="http://xmlgw.companieshouse.gov.uk/v1-0/schema" xsi:schemaLocation="http://xmlgw.companieshouse.gov.uk/v1-0/schema http://xmlgw.companieshouse.gov.uk/v1-0/schema/NameSearch.xsd">
     <ContinuationKey>Some Key</ContinuationKey>
     <RegressionKey>Some Key</RegressionKey>
     <SearchRows>3</SearchRows>
     <CoSearchItem>
        <CompanyName>Company 1</CompanyName>
        <CompanyNumber>1212323</CompanyNumber>
     </CoSearchItem>
     <CoSearchItem>
        <CompanyName>Company 2</CompanyName>
        <CompanyNumber>2122421</CompanyNumber>
     </CoSearchItem>
     <CoSearchItem>
        <CompanyName>Company 3</CompanyName>
        <CompanyNumber>0655421</CompanyNumber>
     </CoSearchItem>
  </NameSearch>
</Body>
</GovTalkMessage>

As I am new to this so I am trying different things. All the following lines return no data,

var v1 = xDocument.Descendants("GovTalkMessage").Elements("CoSearchItem");
var v2 = xDocument.Descendants("Body").Elements("CoSearchItem");
var v3 = xDocument.Descendants("NameSearch").Elements("CoSearchItem");

Kindly guide me how to retrieve Company Name and Company Number for each CoSearchItem node. Thanks

1

There are 1 answers

2
Jonesopolis On BEST ANSWER

Account for the Namespace:

XNamespace ns = "http://xmlgw.companieshouse.gov.uk/v1-0/schema";
var results = xDocument.Descendants(ns + "CoSearchItem")                                
                                  .Select(n => new 
                                  { 
                                       CompanyName = n.Element(ns +"CompanyName").Value, 
                                       CompanyNumber = n.Element(ns +"CompanyNumber").Value 
                                   })
                                   .ToList();