LINQ读取XML
我有这个XML文件
<root> <level1 name="A"> <level2 name="A1" /> <level2 name="A2" /> </level1> <level1 name="B"> <level2 name="B1" /> <level2 name="B2" /> </level1> <level1 name="C" /> </root>
有人可以给我一个C#代码使用LINQ,最简单的方法来打印这个结果:
(注意额外的空间,如果它是一个level2节点)
A A1 A2 B B1 B2 C
目前我得到了这个代码
XDocument xdoc = XDocument.Load("data.xml")); var lv1s = from lv1 in xdoc.Descendants("level1") select lv1.Attribute("name").Value; foreach (var lv1 in lv1s) { result.AppendLine(lv1); var lv2s = from lv2 in xdoc...??? }
尝试这个。
void Main() { StringBuilder result = new StringBuilder(); //Load xml XDocument xdoc = XDocument.Load("data.xml"); //Run query var lv1s = from lv1 in xdoc.Descendants("level1") select new { Header = lv1.Attribute("name").Value, Children = lv1.Descendants("level2") }; //Loop through results foreach (var lv1 in lv1s){ result.AppendLine(lv1.Header); foreach(var lv2 in lv1.Children) result.AppendLine(" " + lv2.Attribute("name").Value); } Console.WriteLine(result); }
或者,如果你想要一个更一般的方法 – 即嵌套到“levelN”:
void Main() { XElement rootElement = XElement.Load(@"c:\events\test.xml"); Console.WriteLine(GetOutline(0, rootElement)); } private string GetOutline(int indentLevel, XElement element) { StringBuilder result = new StringBuilder(); if (element.Attribute("name") != null) { result = result.AppendLine(new string(' ', indentLevel * 2) + element.Attribute("name").Value); } foreach (XElement childElement in element.Elements()) { result.Append(GetOutline(indentLevel + 1, childElement)); } return result.ToString(); }
几个简单的旧的foreach
循环提供了一个干净的解决scheme:
foreach (XElement level1Element in XElement.Load("data.xml").Elements("level1")) { result.AppendLine(level1Element.Attribute("name").Value); foreach (XElement level2Element in level1Element.Elements("level2")) { result.AppendLine(" " + level2Element.Attribute("name").Value); } }
这里有几个基于@bendewey&@dommer示例的完整工作示例。 我需要调整每一个让它工作,但如果另一个LINQ noob正在寻找工作的例子,在这里你去:
//bendewey's example using data.xml from OP using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; class loadXMLToLINQ1 { static void Main( ) { //Load xml XDocument xdoc = XDocument.Load(@"c:\\data.xml"); //you'll have to edit your path //Run query var lv1s = from lv1 in xdoc.Descendants("level1") select new { Header = lv1.Attribute("name").Value, Children = lv1.Descendants("level2") }; StringBuilder result = new StringBuilder(); //had to add this to make the result work //Loop through results foreach (var lv1 in lv1s) { result.AppendLine(" " + lv1.Header); foreach(var lv2 in lv1.Children) result.AppendLine(" " + lv2.Attribute("name").Value); } Console.WriteLine(result.ToString()); //added this so you could see the output on the console } }
接下来:
//Dommer's example, using data.xml from OP using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; class loadXMLToLINQ { static void Main( ) { XElement rootElement = XElement.Load(@"c:\\data.xml"); //you'll have to edit your path Console.WriteLine(GetOutline(0, rootElement)); } static private string GetOutline(int indentLevel, XElement element) { StringBuilder result = new StringBuilder(); if (element.Attribute("name") != null) { result = result.AppendLine(new string(' ', indentLevel * 2) + element.Attribute("name").Value); } foreach (XElement childElement in element.Elements()) { result.Append(GetOutline(indentLevel + 1, childElement)); } return result.ToString(); } }
这些都编译和工作在VS2010中使用csc.exe版本4.0.30319.1并给出完全相同的输出。 希望这些帮助别人正在寻找代码的工作示例。
编辑:添加@eglasius'的例子,因为它变得对我有用:
//@eglasius example, still using data.xml from OP using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; class loadXMLToLINQ2 { static void Main( ) { StringBuilder result = new StringBuilder(); //needed for result below XDocument xdoc = XDocument.Load(@"c:\\deg\\data.xml"); //you'll have to edit your path var lv1s = xdoc.Root.Descendants("level1"); var lvs = lv1s.SelectMany(l=> new string[]{ l.Attribute("name").Value } .Union( l.Descendants("level2") .Select(l2=>" " + l2.Attribute("name").Value) ) ); foreach (var lv in lvs) { result.AppendLine(lv); } Console.WriteLine(result);//added this so you could see the result } }
XDocument xdoc = XDocument.Load("data.xml"); var lv1s = xdoc.Root.Descendants("level1"); var lvs = lv1s.SelectMany(l=> new string[]{ l.Attribute("name").Value } .Union( l.Descendants("level2") .Select(l2=>" " + l2.Attribute("name").Value) ) ); foreach (var lv in lvs) { result.AppendLine(lv); }
PS。 您必须在这些版本中使用.Root。