gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:50:36 31 4
gpt4 key购买 nike

我目前正在使用一个锯齿状数组: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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com