简体   繁体   English

LINQ to XML返回多个子级

[英]LINQ to XML return multiple children

I'm trying to return all the children "APIParamter" from the XML structure below and add the info to some classes. 我正在尝试从下面的XML结构返回所有子级“ APIParamter”,并将信息添加到某些类中。

here is the XML Structure: 这是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>

Here is the class structure: 这是类结构:

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

I have the following code: 我有以下代码:

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

  ;

but when I use this: 但是当我使用这个:

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

I only get the first child when I'm expecting to get 5. I suspect it's my LINQ query but I'm not sure where the problem is. 我只有第一个孩子会在5岁时出生。我怀疑这是我的LINQ查询,但是我不确定问题出在哪里。

PS I have had to cut down the code slightly for posting, just so you are aware of this. PS我不得不稍微减少代码的发布,以便您知道这一点。

You need iterate over the APIParameter children in APIParameters before your select: 在选择之前,您需要遍历APIParameter中的APIParameterAPIParameters

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM