我有这个XML文件:
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Input;
private static readonly byte[] DistinctVirtualKeys = Enumerable
.Range(0, 256)
.Select(KeyInterop.KeyFromVirtualKey)
.Where(item => item != Key.None)
.Distinct()
.Select(item => (byte)KeyInterop.VirtualKeyFromKey(item))
.ToArray();
/// <summary>
/// Gets all keys that are currently in the down state.
/// </summary>
/// <returns>
/// A collection of all keys that are currently in the down state.
/// </returns>
public IEnumerable<Key> GetDownKeys()
{
var keyboardState = new byte[256];
GetKeyboardState(keyboardState);
var downKeys = new List<Key>();
for (var index = 0; index < DistinctVirtualKeys.Length; index++)
{
var virtualKey = DistinctVirtualKeys[index];
if ((keyboardState[virtualKey] & 0x80) != 0)
{
downKeys.Add(KeyInterop.KeyFromVirtualKey(virtualKey));
}
}
return downKeys;
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetKeyboardState(byte[] keyState);
之前,我请求帮助如何从所述XML中获取和提取信息。幸运的是,有些人提供了解决方案。见here。此外,我能够通过使用索引来调用对象(这是正确的术语?)。像这样...... <?xml version="1.0" encoding="utf-8" ?>
<Record>
<File name="slc4t0_appl_release.mot">
<Line address="040004" data="0720" />
<Line address="040037" data="31" />
<Line address="04004C" data="55AA55AA" />
</File>
<File name="SRLCAM4T0_PS_163_780A.PAR.mot">
<Line address="00008242" data="06" />
<Line address="00008025" data="AFC8" />
<Line address="00009302" data="476F6C64" />
</File>
</Record>
现在,我希望能够逐个遍历文件和行。因此,程序首先会经过record.files[0].lines[0].data
然后循环遍历files[0]
,直到完成所有操作,然后再次通过lines[]
再次在其中循环files[1]
,依此类推。
我认为这很容易
files[]
当我尝试它时,它似乎正在做我想要的。但我可能在这里做错了。或者,如果有更好的选择,那就太棒了。
答案 0 :(得分:1)
由于除了访问相应列表中的特定项目之外,您不需要索引i
和ii
,我认为如果您使用{{1},它将更清晰,更易读为此任务:
foreach
根据您目前发布的内容,您的代码似乎已经按照您的描述进行了操作,但我们无法确定您的代码:)
答案 1 :(得分:1)
阅读xml
并访问每个元素的另一种简单方法。
XDocument xml = XDocument.Parse(s);
var q = from b in xml.Root.Descendants("File")
select new
{
File = (string)b.Attribute("name").Value,
Lines = b.Descendants("Line").Select(l=> new { Address= l.Attribute("address").Value, Data = l.Attribute("data").Value})
};
您可以访问此处显示的项目。
foreach (var rec in q)
{
Console.Write("File = {0}", rec.File);
foreach(var line in rec.Lines)
{
Console.Write("Address = {0} , Data = {1}", line.Address, line.Data);
}
}
可以找到工作代码here