XML Linq - 从嵌套的后代中查找 XElement 的值

XML Linq - find a value of XElement from nested Descendants

我有以下来自 REST 调用的 XML 响应的示例

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

我有这个 class 来设置属性:

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; }
    }

现在我有这个 class 到 select 的值:

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")
                                        }
            }
            ;

出于某种原因,IEnumerable AdditionalPoolDetails(PoolId、PoolType、PoolStatus)都返回空值。 谁能帮忙? :)

您必须将最后一个 select 中的 s 替换为 t。另外,最好不要将 Element 转换为 string,而是访问其 Value 属性.

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
}

您可以使用标准序列化

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; }
}