Search code examples
xmllinqlinq-to-xmlienumerablexelement

XML Linq - find a value of XElement from nested Descendants


I have a sample of the following XML response from a REST call

<GetHelloWorldResponse xmlns="http://www.helloworld.com/Services/HelloWorld">
  <Available xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Pool>
      <GameNumber>3081</GameNumber>
      <PoolDate>2022-04-20</PoolDate>
      <Category>MW</Category>
      <ScheduledCloseDate>2022-04-20T13:00:00</ScheduledCloseDate>
      <MinAllowedAST>2022-04-20T00:00:00</MinAllowedAST>
      <MaxAllowedAST>2022-04-20T23:59:00</MaxAllowedAST>
      <Randomised>false</Randomised>
      <Pool xmlns:a="http://www.helloworld.com/Services/Common">
        <a:PoolId>10089269</a:PoolId>
        <a:RaceId>0</a:RaceId>
        <a:FixtureSeq>0</a:FixtureSeq>
        <a:RaceNum>0</a:RaceNum>
        <a:PoolType>FN</a:PoolType>
        <a:PoolStatus>CLOSED</a:PoolStatus>
        <a:PayPlacesCount>0</a:PayPlacesCount>
        <a:OverrideClosedInd>true</a:OverrideClosedInd>

and I have this class to get-set the properties:

public class PoolDetails
    {
        public string GameNumber { get; set; }
        public string PoolDate { get; set; }
        public string Category { get; set; }
        public string ScheduledCloseDate { get; set; }
        public string MinAllowedAST { get; set; }
        public string MaxAllowedAST { get; set; }
        public IEnumerable<AdditionalPoolDetails> AdditionalPoolDetails { get; set; }
    }

    public class AdditionalPoolDetails
    {
        public string PoolId { get; set; }
        public string PoolType { get; set; }
        public string PoolStatus { get; set; }
    }

Now I have this class to select the values:

XDocument xdoc = XDocument.Parse(GetResponseContent(response));
        XNamespace xmlnsa = "http://www.helloworld.com/Services/HelloWorld";
        XNamespace xmlnsb = "http://www.helloworld.com/Services/Common";
        IEnumerable<XElement> available = from xml in xdoc.Descendants(xmlnsa + "Available") select xml;
        IEnumerable<PoolDetails> poolDetails =
            from s in available.Descendants(xmlnsa + "Pool")
            select new PoolDetails()
            {
                GameNumber = (string)s.Element(xmlnsa + "GameNumber"),
                PoolDate = (string)s.Element(xmlnsa + "PoolDate"),
                Category = (string)s.Element(xmlnsa + "Category"),
                ScheduledCloseDate = (string)s.Element(xmlnsa + "ScheduledCloseDate"),
                MinAllowedAST = (string)s.Element(xmlnsa + "MinAllowedAST"),
                MaxAllowedAST = (string)s.Element(xmlnsa + "MaxAllowedAST"),
                AdditionalPoolDetails = from t in s.Descendants(xmlnsa + "Pool") 
                                        select new AdditionalPoolDetails()
                                        {
                                            PoolId = (string)s.Element(xmlnsb + "PoolId"),
                                            PoolType = (string)s.Element(xmlnsb + "PoolType"),
                                            PoolStatus = (string)s.Element(xmlnsb + "PoolStatus")
                                        }
            }
            ;

For some reason, the IEnumerable AdditionalPoolDetails (PoolId, PoolType, PoolStatus) are all returning null. Can anyone help? :)


Solution

  • You have to replace s with t in the last select. Also, it's better not to cast Element to string, but access to its Value property.

    AdditionalPoolDetails = from t in s.Descendants(xmlnsa + "Pool")
    select new AdditionalPoolDetails()
    {
        PoolId = t.Element(xmlnsb + "PoolId")?.Value,
        PoolType = t.Element(xmlnsb + "PoolType")?.Value,
        PoolStatus = t.Element(xmlnsb + "PoolStatus")?.Value
    }