zoukankan      html  css  js  c++  java
  • 对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);
                }
  • 相关阅读:
    exFAT移动硬盘写保护怎么去掉
    ORACLE:一列的多行数据拼成字符串
    cxgrid中,如何根据列名或字段名取得footer值
    Delphi天气预报查询
    Delphi ListView基本用法大全
    datasnap的初步
    Delphi ListView基本用法大全
    Delphi实现树型结构
    获取身份证号码信息
    让Delphi XE5跟其他版本的Delphi共存
  • 原文地址:https://www.cnblogs.com/lunawzh/p/6669753.html
Copyright © 2011-2022 走看看