我試圖從下面的XML結構中返回所有子節點「APIParamter」,並將這些信息添加到某些類中。LINQ to 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.爲了發佈,我不得不略微減少代碼,只是爲了讓你意識到這一點。
這將有助於如果你能削減你的代碼了很多*更*,直到它基本上是一個很小的例子,還說明了問題 - 但最好是一個簡短但完整的程序。 (提示:你可能不需要超過幾個屬性...) –
@David,試過:APIParameters =從p.Descendants(「APIParamter」)中的c選擇新的APIParameters(){Parameter =(string)c。屬性(「參數」),... –
確定Jon Skeet我會在將來嘗試讓我的帖子更好地閱讀。 – David