首页 > 代码库 > c#对xml的操作

c#对xml的操作

 

操作xml可以通过XElement对象,比较方便的使用列举以下几点:

把字符串转变成XElement,保存成xml文件,加载xml文件:

            //把字符串解析成XElement对象            string str = "<students><student><name>one</name></student><student><name>two</name></student><student><name>three</name></student></students>";            var xml = XElement.Parse(str);            //保存文件            xml.Save("1.xml");            Console.WriteLine("保存成功");            //读取文件            xml = XElement.Load("1.xml");            Console.WriteLine(xml);

集合创建XElement对象

class Program    {        static void Main(string[] args)        {             List<Person> list = new List<Person>()            {                new Person(){Id=1,Name="张三"},                new Person(){Id=2,Name="李四"},                new Person(){Id=3,Name="王五"},                new Person(){Id=4,Name="赵六"},                new Person(){Id=5,Name="钱七"}            };            var xml = new XElement("Persons", list.Select(x => new XElement("name",x.Name)));            Console.WriteLine(xml);        }    }    public class Person    {        public int Id { get; set; }        public string Name { get; set; }    }

技术分享

 

2 、使用lamda查询xml值

xml文件如下:

<?xml version="1.0" encoding="utf-8" ?><AOnlineOrg>  <eSales>    <Member SortNo="1">      <Login>GUOXIN</Login>      <PositionID>1-25ISRB</PositionID>      <UserID>1-25ISRQ</UserID>      <BU>ICSF</BU>    </Member>    <Member SortNo="2">      <Login>LIUXIANG</Login>      <PositionID>1-196945</PositionID>      <UserID>1-19694A</UserID>      <BU>ICSF</BU>    </Member>    <Member SortNo="3">      <Login>QINYANG</Login>      <PositionID>1-10DAMN</PositionID>      <UserID>1-KJ3IA</UserID>      <BU>ICSF</BU>    </Member>    <Member SortNo="4">      <Login>XIAOCANFU</Login>      <PositionID>1-FKZUJF</PositionID>      <UserID>1-FKZUB5</UserID>      <BU>ICSF</BU>    </Member>    <Member SortNo="5">      <Login>XIAOHUAHU</Login>      <PositionID>1-JWZ3R</PositionID>      <UserID>1-JWZ4F</UserID>      <BU>ICSF</BU>    </Member>    <Member SortNo="6">      <Login>MANWEIZHAO</Login>      <PositionID>1-JDSX2</PositionID>      <UserID>1-1U5P9V</UserID>      <BU>IAG</BU>    </Member>    <Member SortNo="7">      <Login>LIJUANNIU</Login>      <PositionID>1-IABTN</PositionID>      <UserID>1-1PW39I</UserID>      <BU>IAG</BU>    </Member>    <Member SortNo="8">      <Login>QUANFANGZHOU</Login>      <PositionID>1-H1MFS</PositionID>      <UserID>1-ISATTR</UserID>      <BU>IAG</BU>    </Member>    <Member SortNo="9">      <Login>FENGYUANLI</Login>      <PositionID>1-DHZB5</PositionID>      <UserID>1-DI7M2</UserID>      <BU>IAG</BU>    </Member>  </eSales></AOnlineOrg>

上面xml文件保存为1.xml,查找其Member的属性SortNo的值小于3的Login值:

 //加载            var xml = XElement.Load("1.xml");            //选择集合            var list = xml.Descendants("Member");            //查找xml中Member的属性SortNo的值小于3的Login值            var arr = list.Where(x => ((int)x.Attribute("SortNo")) < 3).Select(x => x.Element("Login"));            foreach (var item in arr)            {                Console.WriteLine(item);            }
查找所有Login值
            var xml = XElement.Load("1.xml");            //查找所有Login值            foreach (var item in xml.Descendants("Login"))            {                Console.WriteLine(item.Value);            }

 

把xml转成类对象集合

 public class Person    {        public int SortNo { get; set; }        public string Login { get; set; }        public string PositionID { get; set; }        public string UserID { get; set; }        public string BU { get; set; }    }
            var xml = XElement.Load("1.xml");            List<Person> list = new List<Person>();                        foreach (var item in xml.Descendants("Member"))            {                if (item.HasElements)                {                    list.Add(new Person                    {                        Login = item.Element("Login").Value,                        PositionID = item.Element("PositionID").Value,                        UserID = item.Element("UserID").Value,                        BU = item.Element("BU").Value,                        SortNo = (int)item.Attribute("SortNo")                    });                }            }            foreach (var item in list)            {                Console.WriteLine(item.Login);            }

 

c#对xml的操作