A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 孟庆波 中级黑马   /  2012-5-6 19:58  /  1273 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace ConsoleApplication1


{
    class Program
    {
        static void Main(string[] args)
        {
            #region;
            //string[] names = { "Burke", "Connor", "Frank", "Everett", "Albert", "George", "Harris", "David" };
            //IEnumerable<string> expr = from s in names
            //                           where s.Length == 5
            //                           orderby s
            //                           select s.ToUpper();
            //foreach (string item in expr)
            //{
            //    this.listView1.Items.Add(item);
            //}


            //Func<int, int, int> add = (int x, int y) => x * y;
            //Console.WriteLine(add(6, 4));
            //Console.ReadKey();

            //Func<int, int, int> add1 = (z, k) => z * k;
            //Console.WriteLine(add1(2, 4));
            //Console.ReadKey();
            //隐含类型的输入参数
            //X => X + 1;
            //X=>{return X+1}
            //costomer=>customer.Name
            //(x,y)=>x*y//多个参数
            //(person,minAge)=>person.Age=>mingAge

            //显示类型的输入参数
            //(int x)=>x+1
            //(int x)=>{return x+1}

            //无参数类型
            //()=>1
            //()=>Console.WriteLine()

            //显示转化为隐式
            //(int x, int y) => x * y;
            //(x, y) => x * y;
            // =>左边为参数,右边为表达式

            #endregion;

            #region;
            //var words = new string[] { "中国", "英国", "法国", "中国人" };
            //IEnumerable<string> hasDAndE =
            //    from s in words
            //    where s.Contains('中') && s.Contains('国')
            //    select s.Trim();


            //var words = new string[] { "中国", "英国", "法国", "中国人" };
            //IEnumerable<string> hasDAndE =
            //words.Where(s => s.Contains('中') && s.Contains('国'));
            //foreach (string s in hasDAndE)
            //{
            //    Console.WriteLine(s);
            //}
            //结果为:中国人,中国

            //排序
            //IEnumerable<int> orderde = numbers.OrderBy(n => n);
            //foreach (var item in orderde)
            //{
            //    Console.WriteLine(item);
            //}

            //string findword = "机";
            //var words = new string[] { "电视机", "电冰箱", "手机" };
            //var matches = words.Select(s => s.Contains(findword));
            //foreach (var str in matches)
            //{
            //    Console.WriteLine(str);
            //}
            //结果true false true

            //string findword = "机";
            //var words = new string[] { "电视机", "电冰箱", "手机" };
            //var matches = words.Where(s => s.Contains(findword));
            //foreach (var str in matches)
            //{
            //    Console.WriteLine(str);

            //}
            #endregion;

            #region;
            //结果:电视机 手机

            //        var recipes = new[] {
            //         "Crepes Florentine", "Shrimp Che Paul",
            //"Beef Burgundy", "Rack of Lamb",
            //"Tacos", "Rosemary Roasted Chicken",
            //"Juevos Rancheros", "Buffalo Chicken Nachos"};

            //        var results = recipes.Where(s => s.Contains("Chicken"));

            //        Array.ForEach<string>(results.ToArray<string>(),
            //                               s => Console.WriteLine(s));
            //((string)item.Attribute("ParNumber"))


            //XElement xelement = XElement.Load(@"C:\Users\mengqingbo\Desktop\linq\WindowsFormsApplication1\ConsoleApplication1\XMLFile1.xml");

            //IEnumerable<string> partNos =
            //    from item in xelement.Descendants("book")
            //    select (string)item.Attribute("type").Value;

            //foreach (string str in partNos)
            //{
            //    Console.WriteLine(str.ToString());
            //}

            //XElement contacts = new XElement("Contacts",
            //new XElement("Name", "lce Lee"),
            //new XElement("Phone", "010-87654632", new XAttribute("Type", "Home")),
            //new XElement("Address", new XElement("Street", "ZhiXinCun"), new XElement("City", "BeiJing"))
            // );
            //Console.WriteLine(contacts);

            //添加节点
            //string xmlPath = @"C:\Users\mengqingbo\Desktop\linq\WindowsFormsApplication1\ConsoleApplication1\XMLFile1.xml";
            //XElement xelement = XElement.Load(xmlPath);
            //XElement newxelement = new XElement("New", "这是新的节点");
            //Console.WriteLine(xelement);
            //xelement.Add(newxelement);
            //Console.WriteLine(xelement);
            ////保存一下
            //xelement.Save(xmlPath);
            #endregion;

            //MyAnnotation ma = new MyAnnotation("T1");
            //XElement root = new XElement("Root", "content");
            //root.AddAnnotation(ma);

            //删除节点
            //string xmlPath = @"C:\Users\mengqingbo\Desktop\linq\WindowsFormsApplication1\ConsoleApplication1\XMLFile1.xml";
            //XElement xelement = XElement.Load(xmlPath);

            //var poNode = from item in xelement.Descendants("new")
            //             select item;
            //poNode.Remove();
            ////保存一下
            //xelement.Save(xmlPath);
            //Console.WriteLine(xelement);

            #region;
            ////更新节点
            ////查找目标树节点的父节点
            ////将先前的节点的孩子转移到新节点
            ////将节点的子节点替换 ReplaceWith:操作本元素
            ////ReplaceNodes ReplaceAttributes ReplaceAll 操作元素的孩子或是属性的内容
            //XElement xel = XElement.Load(@"C:\Users\mengqingbo\Desktop\linq\WindowsFormsApplication1\ConsoleApplication1\XMLFile1.xml");
            //var itemNodes = from item in xel.Descendants("New")
            //                select item;
            //int n = itemNodes.Count();
            //for (int i = 0; i < n; i++)
            //{

            //    //新创建节点
            //    XElement nel = new XElement("Element");
            //    //转移子节点
            //    nel.Add(itemNodes.ElementAt(0).Elements());
            //    //替换
            //    itemNodes.ElementAt(0).ReplaceWith(nel);
            //}
            //Console.WriteLine(xel);

            #endregion;
            Console.ReadKey();

        }


    }

}

xml:
<?xml version="1.0" encoding="utf-8"?>
<BookList>
  <book name="english" type="edu">
    <author>zhangweia</author>
    <price>10</price>
  </book>
  <book name="math" type="edu">
    <author>zhangweib</author>
    <price>20</price>
  </book>
  <book name="math1" type="edu">
    <author>zhangweib</author>
    <price>30</price>
  </book>
  <book name="math2" type="rw">
    <author>zhangweib</author>
    <price>40</price>
  </book>
  <book name="math3" type="rw">
    <author>zhangweib</author>
    <price>50</price>
  </book>
  <book name="math4" type="rw">
    <author>zhangweib</author>
    <price>60</price>
  </book>
  <New>
    这是新的节点
    <meng>hao</meng>
    <qing>ee</qing>
  </New>
</BookList>

评分

参与人数 1技术分 +3 收起 理由
宋天琪 + 3

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马