LINQ to XML return 多个 children

LINQ to XML return multiple children

我正在尝试 return 来自下面 XML 结构的所有 children "APIParamter" 并将信息添加到某些 classes。

这里是 XML 结构:

<?xml version="1.0" encoding="utf-8" ?> 
<API>
<APIColors background="" bordercolor="" textcolor=""></APIColors>
<APIDetails name="Payroll" description="Everything about IQX Payroll">
<![CDATA[Some text        ]]>
</APIDetails>
<APICalls>
          <APICall type="GET" apicalltitle="PayrollNew" apicalldescription="Gets new payroll transactions">
                  <ImplementationNotes note="">text</ImplementationNotes>
                  <APIParameters>
                                <APIParamter parameter="page" description="indicates type of wervice - always this value" parametertype="" datatype="string" filename="" required="YES">J_PayrollNew</APIParamter>  
                                <APIParamter parameter="render" description="response type required - always text_xml" parametertype="" datatype="string" filename="" required="YES">text_xml</APIParamter>  
                                <APIParamter parameter="auth" description="userid and password separated by colon" parametertype="" datatype="string" filename="" required="YES">userid:password</APIParamter>  
                                <APIParamter parameter="pPayrollCompany" description="optionally limits results to specified payroll company" parametertype="" datatype="string" filename="" required="NO">value</APIParamter>  
                                <APIParamter parameter="pTransactionType" description="optionally limits results to specified transaction type. 0 for employee, 2 for timesheet" parametertype="" datatype="numeric" filename="" required="NO">value</APIParamter>  
                  </APIParameters>
          </APICall>
</APICalls>
</API>

这里是 class 结构:

public class TheIQXAPIs
{
    public string Name { get; set; }
    public string Description { get; set; }
    public IEnumerable<APICalls> APICalls { get; set; }
    public string Notes { get; set; }
}

public class APICalls
{
    public string Type { get; set; } //Post, get etc.
    public string APICallTitle { get; set; }
    public string APICallDescription { get; set; }
    public string ImplementationNote { get; set; }
    public IEnumerable<APIParameters> APIParameters { get; set; }
    public IEnumerable<ResponseMessages> ResponseMessages { get; set; }
    public string RequestURL { get; set; }
    public string ResponseBody { get; set; }
    public string ResponseCode { get; set; }
    public string ResponseHeaders { get; set; }

}

public class APIParameters
{

    public string Parameter { get; set; }
    public string Value { get; set; }
    public string Description { get; set; }
    public string ParameterType { get; set; }
    public string DataType { get; set; }
    public string FileName { get; set; }
    public string Required { get; set; }
}

我有以下代码:

IEnumerable<TheIQXAPIs> TheIQXAPIs = 
  from e in doc.Descendants("API")
  select new TheIQXAPIs()
  {
      Notes = (string)e.Element("APIDetails"),
      Name = e.Element("APIDetails").Attribute("name").Value,
      Description = e.Element("APIDetails").Attribute("description").Value,   
      APICalls = from p in e.Descendants("APICall")
                 select new APICalls() 
                 {
                     Type = (string)p.Attribute("type"),
                     APICallTitle = (string)p.Attribute("apicalltitle"),
                     APICallDescription = (string)p.Attribute("apicalldescription"),
                     ImplementationNote = (string)p.Element("ImplementationNotes"),
                     APIParameters = from c in p.Descendants("APIParameters")
                                      select new APIParameters() 
                                        {

                                            Parameter = (string)c.Element("APIParamter").Attribute("parameter"),
                                            Value = (string)c.Element("APIParamter"),
                                            Description = (string)c.Element("APIParamter").Attribute("description"),
                                            ParameterType = (string)c.Element("APIParamter").Attribute("parametertype"),
                                            DataType = (string)c.Element("APIParamter").Attribute("datatype"),
                                            FileName = (string)c.Element("APIParamter").Attribute("filename"),
                                            Required = (string)c.Element("APIParamter").Attribute("required")
                                        }
                    }
  }

  ;

但是当我使用这个时:

foreach (TheIQXAPIs TheAPI in TheAPIs)
{
    foreach (APICalls TheAPICall in TheAPI.APICalls)
    { 
        foreach (APIParameters APIParam in TheAPICall.APIParameters)
        {
            TheLabel.Text = APIParam.Parameter;
            TheLabel2.Text = APIParam.Value;
            etc...      
        }
    }
}

我只得到第一个 child,而我希望得到 5 个。 我怀疑这是我的 LINQ 查询,但我不确定问题出在哪里。

P.S。为了发布,我不得不稍微减少代码,以便您了解这一点。

您需要在 select:

之前迭代 APIParameters 中的 APIParameter children
APIParameters = from c in p.Descendants("APIParameter")
       select new APIParameters() 
       {
            Parameter = (string)c.Attribute("parameter"),
            Value = (string)c.Value,
            Description = (string)c.Attribute("description"),
            ParameterType = (string)c.Attribute("parametertype"),
            DataType = (string)c.Attribute("datatype"),
            FileName = (string)c.Attribute("filename"),
            Required = (string)c.Attribute("required")
         }