C#学习之XML、委托

1.创建XML文档

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace _01
{
    class Program
    {
        static void Main(string[] args)
        {
            //通过代码来创建XML文档
            //1、引用命名空间
            //2、创建XML文档对象
            XmlDocument doc = new XmlDocument();
            //3、创建第一个行描述信息,并且添加到doc文档中
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);
            //4、创建根节点
            XmlElement books = doc.CreateElement("Books");
            //将根节点添加到文档中
            doc.AppendChild(books);

            //5、给根节点Books创建子节点
            XmlElement book1 = doc.CreateElement("Book");
            //将book添加到根节点
            books.AppendChild(book1);


            //6、给Book1添加子节点
            XmlElement name1 = doc.CreateElement("Name");
            name1.InnerText = "西游记";
            book1.AppendChild(name1);

            XmlElement price1 = doc.CreateElement("Price");
            price1.InnerText = "10";
            book1.AppendChild(price1);

            XmlElement des1 = doc.CreateElement("Des");
            des1.InnerText = "好看";
            book1.AppendChild(des1);

            XmlElement book2 = doc.CreateElement("Book");
            books.AppendChild(book2);


            XmlElement name2 = doc.CreateElement("Name");
            name2.InnerText = "西游记";
            book2.AppendChild(name2);

            XmlElement price2= doc.CreateElement("Price");
            price2.InnerText = "10";
            book2.AppendChild(price2);

            XmlElement des2 = doc.CreateElement("Des");
            des2.InnerText = "好看";
            book2.AppendChild(des2);

            doc.Save("Books.xml");
            Console.WriteLine("保存成功");
            Console.ReadKey();
        }
    }
}

2.带属性的XML

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace _02
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8","yes");
            doc.AppendChild(dec);

            XmlElement order = doc.CreateElement("Order");
            doc.AppendChild(order);

            XmlElement customerName = doc.CreateElement("CustomerName");
            customerName.InnerXml = "<p>我是一个p标签</p>";
            order.AppendChild(customerName);

            XmlElement customerNumber = doc.CreateElement("CustomerNumber");
            customerNumber.InnerText = "<p>我是一个p标签</p>";
            order.AppendChild(customerNumber);


            XmlElement items = doc.CreateElement("Items");
            order.AppendChild(items);

            XmlElement orderItem1 = doc.CreateElement("OrderItem");
            //给节点添加属性
            orderItem1.SetAttribute("Name", "牛奶");
            orderItem1.SetAttribute("Count", "10");
            items.AppendChild(orderItem1);

            XmlElement orderItem2 = doc.CreateElement("OrderItem");
            //给节点添加属性
            orderItem2.SetAttribute("Name", "牛奶");
            orderItem2.SetAttribute("Count", "10");
            items.AppendChild(orderItem2);

            XmlElement orderItem3 = doc.CreateElement("OrderItem");
            //给节点添加属性
            orderItem3.SetAttribute("Name", "牛奶");
            orderItem3.SetAttribute("Count", "10");
            items.AppendChild(orderItem3);

            doc.Save("Order.xml");
            Console.WriteLine("保存成功");
            Console.ReadKey();

            
        }
    }
}

3.读取XML

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace _03
{
    class Program
    {
        static void Main(string[] args)
        {
            //XmlDocument doc = new XmlDocument();
            加载要读取的XML
            //doc.Load("Books.xml");

            获得根节点
            //XmlElement books = doc.DocumentElement;

            获得子节点 返回节点的集合
            //XmlNodeList xnl = books.ChildNodes;

            //foreach (XmlNode item in xnl)
            //{
            //    Console.WriteLine(item.InnerText);
            //}
            //Console.ReadKey();


            //读取带属性的XML文档

            //XmlDocument doc = new XmlDocument();
            //doc.Load("Order.xml");
            //Xpath

            //XmlDocument doc = new XmlDocument();
            //doc.Load("Order.xml");
            //XmlNodeList xnl = doc.SelectNodes("/Order/Items/OrderItem");

            //foreach (XmlNode node in xnl)
            //{
            //    Console.WriteLine(node.Attributes["Name"].Value);
            //    Console.WriteLine(node.Attributes["Count"].Value);
            //}
            //Console.ReadKey();
            //改变属性的值
            //XmlDocument doc = new XmlDocument();
            //doc.Load("Order.xml");
            //XmlNode xn = doc.SelectSingleNode("/Order/Items/OrderItem[@Name='190']");
            //xn.Attributes["Count"].Value = "200";
            //xn.Attributes["Name"].Value = "李四";
            //doc.Save("Order.xml");
            //Console.WriteLine("保存成功");




            XmlDocument doc = new XmlDocument();

            doc.Load("Order.xml");

            XmlNode xn = doc.SelectSingleNode("/Order/Items");

            xn.RemoveAll();
            doc.Save("Order.xml");
            Console.WriteLine("删除成功");
            Console.ReadKey();

            获得文档的根节点
            //XmlElement order = doc.DocumentElement;
            //XmlNodeList xnl = order.ChildNodes;
            //foreach (XmlNode item in xnl)
            //{
            //    如果不是Items 就continue
            //    //if (item[])
            //    //{
            //    //    continue;
            //    //}
            //    Console.WriteLine(item.Attributes["Name"].Value);
            //    Console.WriteLine(item.Attributes["Count"].Value);
            //}
            Console.ReadKey();
        }
    }
}

4.委托使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _04
{
    public delegate string DelProStr(string name);
    class Program
    {
        static void Main(string[] args)
        {
            //三个需求
            //1、将一个字符串数组中每个元素都转换成大写
            //2、将一个字符串数组中每个元素都转换成小写
            //3、将一个字符串数组中每个元素两边都加上 双引号
            string[] names = { "abCDefG", "HIJKlmnOP", "QRsTuvW", "XyZ" };
            //ProStToUpper(names);
            //ProStrToLower(names);
            // ProStrSYH(names);

            ProStr(names, delegate(string name)
            {
                return "\"" + name + "\"";
            });
            for (int i = 0; i < names.Length; i++)
            {
                Console.WriteLine(names[i]);
            }
            Console.ReadKey();
        }


        public static void ProStr(string[] name, DelProStr del)
        {
            for (int i = 0; i < name.Length; i++)
            {
                name[i] = del(name[i]);
            }
        }

        //public static string StrToUpper(string name)
        //{
        //    return name.ToUpper();
        //}

        //public static string StrToLower(string name)
        //{
        //    return name.ToLower();
        //}

        //public static string StrSYH(string name)
        //{
        //    return "\"" + name + "\"";
        //}


        //public static void ProStToUpper(string[] name)
        //{
        //    for (int i = 0; i < name.Length; i++)
        //    {
        //        name[i] = name[i].ToUpper();
        //    }
        //}
        //public static void ProStrToLower(string[] name)
        //{
        //    for (int i = 0; i < name.Length; i++)
        //    {
        //        name[i] = name[i].ToLower();
        //    }
        //}
        //public static void ProStrSYH(string[] names)
        //{
        //    for (int i = 0; i < names.Length; i++)
        //    {
        //        names[i] = "\"" + names[i] + "\"";
        //    }
        //}


    }
}

5. 匿名函数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _05
{
    public delegate void DelSayHi(string name);
    class Program
    {
        static void Main(string[] args)
        {
            //SayHi("张三", SayHiChinese);
            //DelSayHi del = delegate(string name)
            //{
            //    Console.WriteLine("你好" + name);
            //};
            //del("张三");


            //lamda表达式  => goes to
            DelSayHi del = (string name) => { Console.WriteLine("你好" + name); };
            del("张三");
            Console.ReadKey();
        }

        //public static void SayHi(string name,DelSayHi del)
        //{
        //    del(name);
        //}

        //public static void SayHiChinese(string name)
        //{
        //    Console.WriteLine("你好"+name);
        //}
        //public static void SayHiEnglish(string name)
        //{
        //    Console.WriteLine("Hello"+name);
        //}
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

挑战不可

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值