本人开发的开发者技术变现资源聚集地,大家支持下,下面是网址
https://www.baiydu.com
使用Linq可以用更小的语句查询XML中的节点,这的确比以前方便多了,在网页: 中介绍了相关的例子,写成程序显示一下吧。程序操作的XML文件位于: 程序代码: using System; using System.Collections.Generic; using System.Linq; using System.Xml.Linq;
public sealed class Programs { public static void Main() { //获取采购单每个项元素的部件号属性值 var purchaseOrder = XElement.Load("PurchaseOrder.xml", LoadOptions.None); IEnumerable<string> partNos = from item in purchaseOrder.Descendants("Item") select (string)item.Attribute("PartNumber"); // 注意,这里可以进行显式类型转换。
foreach (var num in partNos) Console.WriteLine(num);
// 一个列表,列出值大于 100 美元的项,并根据部件号排序 IEnumerable<XElement> partNos2 = from item in purchaseOrder.Descendants("Item") where (int)item.Element("Quantity") * (decimal)item.Element("USPrice") > 100 orderby (string)item.Element("ProductName") select item;
foreach (var node in partNos2) Console.WriteLine((String)node.Element("ProductName")); } } 在上面的代码中,注意几点: 1:Descendants()里面要求提供的是XName参数,但如果提供了String 会直接进行隐式转换。 2:Descendants()可以不仅可以是下级元素,还可以是下下级元素。 3:为更快地获得元素的内容,可以直接进行类型转换,这很方便! 运行效果:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace ExtendMethodsTestOfIEnumerable
{
class Program
{
static void Main(string[] args)
{
IEnumerable<string> list = new List<string>() { "1", "3", "5", "7", "9", "2", "4", "6", "8", "10" };
string list1 = list.Aggregate<string>(delegate(string arg1, string arg2)
{
return arg1 + "*" + arg2;
});
Console.WriteLine("IEnumerable<string>.Aggregate " + list1);
bool list2 = list.All<string>(delegate(string element)
{
if (element.CompareTo("5") > 0) return true;
else return false;
});
Console.WriteLine("IEnumerable<string>.All " + list2);
bool list3 = list.Any<string>(delegate(string element)
{
if (element.CompareTo("5") > 0) return true;
else return false;
});
Console.WriteLine("IEnumerable<string>.Any " + list3);
double list4 = list.Average<string>(delegate(string element)
{
if (element != null) return element.Length;
return 0;
});
Console.WriteLine("IEnumerable<string>.Average " + list4);
int list5 = list.Count<string>(delegate(string element)
{
if (element.Length > 5) return true;
else return false;
});
Console.WriteLine("IEnumerable<string>.Count " + list5);
string list6 = list.First<string>(delegate(string element)
{
if (element.Length > 1) return true;
return false;
});
Console.WriteLine("IEnumerable<string>.First " + list6);
string list7 = list.FirstOrDefault<string>(delegate(string element)
{
if (element.Length > 2) return true;
return false;
});
Console.WriteLine("IEnumerable<string>.FirstOrDefault " + list7);
IEnumerable<IGrouping<int,string>> list8= list.GroupBy<string, int>(delegate(string element)
{
if (element.Length > 1) return 2;
return 1;
});
Console.WriteLine("IEnumerable<string>.GroupBy: ");
foreach (IGrouping<int, string> element in list8)
{
Console.WriteLine("\t" + element.Key + ":" + element.Aggregate<string>((x, y) => x + y));
}
List<string> other = new List<string>() {"xxx","yyy","zzz","new" };
IEnumerable<string> list9= list.GroupJoin<string, string, int, string>(other,
delegate(string element) { if (element.Length > 1) return 2; return 1; },
delegate(string element) { if (element.CompareTo("yyy") <= 0) return 1; return 2; },
delegate(string element, IEnumerable<string> resultOfOther)
{
return element + resultOfOther.Aggregate<string>((x, y) => x + y);
});
Console.WriteLine("IEnumerable<string>.GroupJoin:");
foreach (string element in list9)
{
Console.WriteLine("\t" + element);
}
IEnumerable<string> list10= list.Join<string, string, int, string>(other,
delegate(string element) { if (element.Length > 1) return 2; return 1; },
delegate(string element) { if (element.CompareTo("yyy") <= 0) return 1; return 2; },
delegate(string arg1, string arg2)
{
return arg1 + arg2;
});
Console.WriteLine("IEnumerable<string>.Join:\n\t" + list10.Aggregate<string>((x, y) => x + " " + y));
IEnumerable<string> list11 = list.OrderBy<string, int>(delegate(string element)
{
return Convert.ToInt32(element);
});
Console.WriteLine("IEnumerable<string>.OrderBy:\n\t" + list11.Aggregate<string>((x, y) => x + " " + y));
IEnumerable<string> list12= list.Select<string, string>(delegate(string element)
{
return "a" + element[0];
});
Console.WriteLine("IEnumerable<string>.Select:\n\t" + list12.Aggregate<string>((x, y) => x + " " + y));
IEnumerable<string> list13= list.SelectMany<string, string>(delegate(string element)
{
if (element.Length > 1) return new string[] { "xxx", "yyy" };
return new string[] { element };
});
Console.WriteLine("IEnumerable<string>.SelectMany:\n\t" + list13.Aggregate<string>((x, y) => x + " " + y));
int list14 = list.Sum<string>(delegate(string element) { return element.Length; });
Console.WriteLine("IEnumerable<string>.Sum:" + list14);
IEnumerable<string> list15= list.Zip<string, string, string>(other,
delegate(string arg1, string arg2)
{
return arg1 + arg2;
});
Console.WriteLine("IEnumerable<string>.Zip:\n\t" + list15.Aggregate<string>((x, y) => x + y));
Console.Read();
}
}
}