gpt4 book ai didi

c# - XDocument 加载到 ToList

转载 作者:太空宇宙 更新时间:2023-11-03 21:17:20 24 4
gpt4 key购买 nike

我正在借助 XDocument 加载 .xml 文件。我成功读取了 .xml 文件。

C#代码:

XDocument doci = XDocument.Load(path);
var mijav = from r in doci.Descendants("Configuration").Descendants("DayRoutine").Descendants("DayRoutine").Where(r => (int)r.Attribute("ID") == 4)
select new
{
Button = r.Element("Button").Value,
DataPoints = r.Elements("DayRoutinePoints").Select(c => (string)c.Value).ToList(),
};

我遇到的问题是 DataPoint 变量。我在“一个”数组中只得到一个值,所有的点都写在这个数组中。如何为每个读取的行划分这些数据?

现在的数据点变量:

"00:00:00, 44004:45:00, 48013:35:00, 60015:00:00, 41519:55:00, 600"

XML 中的点数据以及我喜欢的方式:

"00:00:00, 440
04:45:00, 480
13:35:00, 600
15:00:00, 415
19:55:00, 600"

我的 XML 文件:

<blabla>
<Infos>
<ConfigurationName>XXConfigurationName</ConfigurationName>
<DateSaved>14.10.2015 13:14:01</DateSaved>
</Infos>
<Configuration>
<DayRoutine>
<DayRoutine ID="4">
<Button>1</Button>
<SetupOption>StaticBasic_DoffEoff</SetupOption>
<DayRoutinePoints>
<Point0>00:00:00, 440</Point0>
<Point1>04:45:00, 480</Point1>
<Point2>13:35:00, 600</Point2>
<Point3>15:00:00, 415</Point3>
<Point4>19:55:00, 600</Point4>
</DayRoutinePoints>
</DayRoutine>
</DayRoutine>
</Configuration>
</blabla>

最佳答案

试试这个:

XDocument doci = XDocument.Load(path);
var mijav =
doci.Descendants("Configuration")
.Descendants("DayRoutine")
.Descendants("DayRoutine")
.Where(r => (int) r.Attribute("ID") == 4)
.Select(r => new
{
Button = r.Element("Button").Value,
DataPoints =
r.Elements("DayRoutinePoints").Elements()
.Select(c => (string) c.Value)
.ToList(),
});

关于c# - XDocument 加载到 ToList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33125528/

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