给出以下XML文件,
- <CommandTiming Name="Architecture.Dome">
<Sample Name="run1" EllapsedTime="627" />
<Sample Name="run2" EllapsedTime="636" />
<Sample Name="run3" EllapsedTime="650" />
<Sample Name="run4" EllapsedTime="626" />
<Sample Name="run5" EllapsedTime="643" />
</CommandTiming>
我想在runTiming Name =“Architecture.Dome”中提取run1,run2,run3,run4和run5的EllapsedTime
我想要一个值为627,636,650,626,643的数组。
感谢。
答案 0 :(得分:1)
这应该这样做:
XDocument doc = XDocument.Parse(
"<CommandTiming Name='Architecture.Dome'>" +
"<Sample Name='run1' EllapsedTime='627' />" +
"<Sample Name='run2' EllapsedTime='636' /></CommandTiming>");
var values = doc.XPathSelectElements("CommandTiming[@Name='Architecture.Dome']/Sample")
.Select(v => v.Attribute("EllapsedTime").Value);
答案 1 :(得分:0)
尝试这样的事情:
XDocument doc = XDocument.Parse(
"<CommandTiming Name='Architecture.Dome'>" +
"<Sample Name='run1' EllapsedTime='627' />" +
"<Sample Name='run2' EllapsedTime='636' /></CommandTiming>");//etc
var elapsedTimes = doc.Root.Elements().Attributes("EllapsedTime").
Select(e => e.Value).ToArray<string>();