对xml的操作使用的类XElement的使用

操作xml的类比较多,发现XElement类操作xml极其方便,下面列举一些操作方法

1、创建xml

            XElement xml = new XElement("root", 
                new XElement("Parent", 
                    new XElement("Me",
                        new XElement("son"),
                        new XElement("daughter")
                )));

2、用Lambda表达式

            List<string> list = new List<string>(){
                "Parent","Me","son"
            };

            XElement xml=new XElement("root",
                list.Select(x=>new XElement(x))
                );

            Console.WriteLine(xml);
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("Name", "zhangsan");
            dic.Add("Sex", "");
            dic.Add("Age", "18");
            XElement xml=new XElement("root",
                dic.Select(x=>new XElement(x.Key,x.Value))
                );

            Console.WriteLine(xml);

3、用字符串创建 ,这种我认为最为简单

            string str = "<Parent><Me>张三</Me></Parent>";
            XElement xml = XElement.Parse(str);

            Console.WriteLine(xml);

4、属性

            XElement xml = new XElement("students", 
                new XElement("student", 
                    new XAttribute("id", "1"), 
                    new XElement("name", "张三"), 
                    new XElement("age", 12)));
            string str = "<students><student id="1"><name>张三</name><age>12</age></student></students>";
            XElement xml = XElement.Parse(str);

            Console.WriteLine(xml);

5、保存 xml

            string str = "<students><student id="1"><name>张三</name><age>12</age></student></students>";
            XElement xml = XElement.Parse(str);

            xml.Save("test.xml");

6、加载

            XElement xml = XElement.Load("test.xml");
            Console.WriteLine(xml);

7、读取 xml

   
。。。。。{

 XElement xml = XElement.Load("test.xml");
           
            ForXml(xml);
        }

        public static void ForXml(XElement x)
        {
            printXml(x);
            foreach (var item in x.Elements())
            {
                ForXml(item);
            }
        }
        public static void printXml(XElement x)
        {
            if (x == null)
                return;
            if (x.HasElements)
            {
                Console.WriteLine(x.Name);
            }
            else
            {
                Console.WriteLine(x.Name+":"+x.Value);
            }
            foreach (XAttribute attr in x.Attributes())
            {
                Console.WriteLine("	"+attr.Name+":"+attr.Value);
            }
        }

8、查找某一值

            XElement xml = XElement.Load("test.xml");

            var item = xml.Descendants().Where(x =>
              {
                  var attr = (int?)x.Attribute("id");
                  if (attr != null)
                  {
                      if (attr.Value == 1)
                          return true;
                  }
                  return false;
                  //if (x.Value == "张三")
                  //{
                  //    return true;
                  //}
                  //else
                  //{
                  //    return false;
                  //}
              });
            foreach (XElement x in item)
            {
                ForXml(x);
            }
原文地址:https://www.cnblogs.com/lunawzh/p/6669753.html