我试过但是无法得到它?这是xml文件
<Root>
<Data>
<Keys>Key1, key2, key3, key4</Keys>
<FirstRow>Key1row1, Key2Row1, Key3Row1, Key4Row1</FirstRow>
<SecondRow>Key1Row2, Key2Row2, Key3Row2, Key4Row2</SecondRow>
<ThirdRow>Key1Row3, Key2Row3, Key2Row3, Key2Row3</ThirdRow>
and so on .........
</Data>
</Root>
我需要将Linq用于XML: -
var data = from d in xDoc.Root.Element("Data")
select d;
foreach(var d in data)
{
...
}
我喜欢将以逗号分隔键值的M-d数组映射与以逗号分隔行值。
Key1 key2 key3 key4
Key1row1 key2Row1 Key3Row1 Key4Row1
....
答案 0 :(得分:0)
可以迭代行元素(如已完成),但它会使用string.Split
(或更高级的解析器)来分解行值本身。在以下示例中,它使用RowTextToValues
方法完成。
这是一个LINQPad示例(运行为&#34; C#Program&#34;)并附带一些提示:
IEnumerable<string> RowTextToValues (string v) {
return v.Split(new [] {", "}, StringSplitOptions.None);
}
XDocument TestDoc() {
return XDocument.Parse(@"
<Root>
<Data>
<Keys>Key1, key2, key3, key4</Keys>
<FirstRow>Key1row1, Key2Row1, Key3Row1, Key4Row1</FirstRow>
<SecondRow>Key1Row2, Key2Row2, Key3Row2, Key4Row2</SecondRow>
<ThirdRow>Key1Row3, Key2Row3, Key2Row3, Key2Row3</ThirdRow>
</Data>
</Root>");
}
void Main()
{
var dataNode = TestDoc().Root.Element("Data");
// -> IEnumerable<XElement>
var rows = dataNode.Elements();
// -> IEnumerable<IEnumerable<string>>
var res = rows
.Select(r => RowTextToValues(r.Value));
res.Dump();
}
转换为二维数组或处理更复杂的规则留作练习。