我有一个方法:
private int[] ParseBusyPlace(string xml)
{
var busyPlaces = from element in XDocument.Parse(xml).Descendants("Place")
select new []
{
(int) element.Attribute("ID")
};
return busyPlaces; // error
}
Run Code Online (Sandbox Code Playgroud)
此查询返回IEnumerable<int[]>.如何返回数组int?
Özg*_*ara 10
使用
private int[] ParseBusyPlace(string xml)
{
var busyPlaces = from element in XDocument.Parse(xml).Descendants("Place")
select (int) element.Attribute("ID");
return busyPlaces.ToArray();
}
Run Code Online (Sandbox Code Playgroud)
ÖzgürKara的答案是绝对正确的,但我会重写它不使用查询表达式,因为它只是来自/ select:
private int[] ParseBusyPlace(string xml)
{
return XDocument.Parse(xml)
.Descendants("Place")
.Select(element => (int) element.Attribute("ID"))
.ToArray();
}
Run Code Online (Sandbox Code Playgroud)
我也会考虑返回一个List<int>数组,而不是一个数组,因为它们通常更令人愉快,但这是另一回事.