Home > Software design >  XML Linq - find a value of XElement from nested Descendants
XML Linq - find a value of XElement from nested Descendants

Time:04-21

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

CodePudding user response:

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
}

CodePudding user response:

you can use a standard seserialization

void Main()
{
    string str = "<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></Pool></Pool></Available></GetHelloWorldResponse>";

    XDocument xDoc = XDocument.Parse(str);
    var xns = new System.Xml.Serialization.XmlSerializerNamespaces();
    xns.Add("", "http://www.helloworld.com/Services/HelloWorld");
    xns.Add("a", "http://www.helloworld.com/Services/Common");
    System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(GetHelloWorldResponse));
    GetHelloWorldResponse a = serializer.Deserialize(xDoc.Root.CreateReader()) as GetHelloWorldResponse;
    a.Dump();
}

public static class Ext
{
    public static T Deserialize<T>(String aXml)
    {
        T obj = Activator.CreateInstance<T>();

        using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(aXml.Replace("\\", ""))))
        {
            try
            {
                XmlReader objXR = new XmlTextReader(ms);
                XmlSerializer serializer = new XmlSerializer(obj.GetType());
                obj = (T)serializer.Deserialize(objXR);
            }
            catch (InvalidOperationException ioex)
            {
                string sSource;
                string sLog;
                string sEvent;

                sSource = "News Feed";
                sLog = "Application";
                sEvent = "Deserialize(): An error occured while deserializing an XML feed file.  "   ioex.Message;

                if (!EventLog.SourceExists(sSource))
                {
                    EventLog.CreateEventSource(sSource, sLog);
                }

                EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Warning);
            }

            return obj;
        }
    }
}


[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.helloworld.com/Services/HelloWorld")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.helloworld.com/Services/HelloWorld", IsNullable = false)]
public partial class GetHelloWorldResponse
{
public GetHelloWorldResponseAvailable Available {get;set;}
}


[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.helloworld.com/Services/HelloWorld")]
public partial class GetHelloWorldResponseAvailable
{
    public GetHelloWorldResponseAvailablePool Pool {get;set;} 
}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.helloworld.com/Services/HelloWorld")]
public partial class GetHelloWorldResponseAvailablePool
{
    public ushort GameNumber {get;set;}

    [System.Xml.Serialization.XmlElementAttribute(DataType = "date")]
    public System.DateTime PoolDate {get;set;}
    public string Category {get;set;}
    public System.DateTime ScheduledCloseDate {get;set;}
    public System.DateTime MinAllowedAST {get;set;}
    public System.DateTime MaxAllowedAST {get;set;}
    public bool Randomised {get;set;}
    public GetHelloWorldResponseAvailablePoolPool Pool {get;set;}
}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.helloworld.com/Services/HelloWorld")]
public partial class GetHelloWorldResponseAvailablePoolPool
{
    [System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.helloworld.com/Services/Common")]
    public uint PoolId {get;set;}
    [System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.helloworld.com/Services/Common")]
    public byte RaceId {get;set;}
    [System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.helloworld.com/Services/Common")]
    public byte FixtureSeq {get;set;}
    [System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.helloworld.com/Services/Common")]
    public byte RaceNum {get;set;}
    [System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.helloworld.com/Services/Common")]
    public string PoolType {get;set;}
    [System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.helloworld.com/Services/Common")]
    public string PoolStatus {get;set;}
    [System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.helloworld.com/Services/Common")]
    public byte PayPlacesCount {get;set;}
    [System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.helloworld.com/Services/Common")]
    public bool OverrideClosedInd {get;set;}
}

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

enter image description here

  • Related