LINQ to XML返回多个子节点

时间:2015-04-07 12:12:37

标签: c# linq-to-xml

我试图归还所有的孩子" APIParamter"从下面的XML结构中,将信息添加到某些类中。

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

这是类结构:

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

当我期待获得5分时,我才会得到第一个孩子。 我怀疑这是我的LINQ查询,但我不确定问题出在哪里。

P.S。我不得不稍微减少发布的代码,只是让你意识到这一点。

1 个答案:

答案 0 :(得分:0)

您需要在选择之前迭代APIParameter APIParameters个孩子:

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