c# - 使用 linq 将 xml 文件加载到二维矩形数组中

标签 c# xml arrays linq

我目前正在使用锯齿状数组:private double[][] array;并且我像这样加载数组。

加载锯齿状数组

XDocument doc = XDocument.Load("file.Xml");
array = doc.Root.Elements("Month").Select(month => month.Elements().Select(x => (double)x).ToArray()).ToArray();

所以现在我的问题是我需要获取内部数组的长度,正如我所读到的,这是不可能的,所以我需要将 xml 加载到矩形数组中 private double[,] array; 但对于 linq 我不知道如何。

XML 外观

<document>
  <Month>
    <Depth>-0.25</Depth>
    <October>0.95</October>
    <November>-0.90</November>
    ...
  </Month>
  <Month>
    <Depth>-0.5</Depth>
    <October>0.47</October>
    <November>-0.17</November>
    ...
  </Month>
  ...
</document>

最佳答案

double[][] array = 
   doc.Root.Elements("Month")
      .Select(month => month.Elements().Select(x => (double)x).ToArray())
      .ToArray();

foreach(var innerArray in array)
    Console.WriteLine(innerArray.Length); // you can get length of inner array

关于c# - 使用 linq 将 xml 文件加载到二维矩形数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21554284/

相关文章:

php - 在 if 语句中使用 in_array() 在导致 "Maximum execution time of 30 seconds exceeded"的循环中返回 false

c# - 如何过滤掉国家代码?

c# - LINQ toEntity - 使用 IQueryable.Contains 过滤数据

c# - c# 中的 const 字典

java - 如何在android中创建自定义开关控件

javascript - 如何检查用户输入是否等于数组中的某个值?

c# - DLL 导入中的 SafeFileHandle

angularjs - 在 AngularJS 测试中是否有比 Protractor/XPath 更好的替代方案?

c# - 如何在 C# 中添加自定义标签将 JSON 转换为 XML

Javascript 抽认卡学习应用程序