遍历XML
我们用linq to xml 遍历xml文档可以用以下三种方法,如下代码:
static void Main(string[] args)
{
XElement xel = XElement.Parse(@"
<Root>
<Persons>
<Person>Huang Cong</Person>
<Person>Zhang San</Person>
<Person>Li Si</Person>
<Person>Wang Wu</Person>
</Persons>
</Root>");
Console.WriteLine("Used XNode:");
foreach (XNode n in xel.Nodes())
{
Console.WriteLine(n);
}
Console.WriteLine("-");
Console.WriteLine("Used XElement:");
foreach (XElement e in xel.Elements("Persons"))
{
Console.WriteLine(e);
}
Console.WriteLine("-");
Console.WriteLine("Used Lambda:");
var query = xel.Elements().Select(u => u);
foreach (var q in query)
{
Console.WriteLine(q);
}
}