Я пытаюсь вернуть все дочерние элементы «APIParamter» из структуры XML ниже и добавить информацию к некоторым классам.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 = from c in p.Descendants («APIParamter») выбрать новые APIParameters() {Parameter = (string) c. Атрибут («параметр»), ... –
OK Джон Скит Я буду в дальнейшем пытаться читать мое сообщение лучше. – David