zoukankan      html  css  js  c++  java
  • C#Xml的三种创建方式(或者是两种?)和增删改查

    一.Xml的创建方式

    点击查看代码
    	XElement xElement = new XElement(
    			  new XElement("ProductType",
    				  new XElement("BMW",
    					  new XElement("Threshold", "Search", new XAttribute("Max", 100)),//后面这个是obj类型,写啥都行,输出的时候XAttribute.Value都是string类型
    					  new XElement("Threshold", "Search", new XAttribute("Min", "20")),
    					  new XElement("ROI", "Rect1"),
    					  new XElement("Date", DateTime.Now.ToString("yyyy-MM-dd"))
    					  ),
    				   new XElement("Volvo",
    					new XElement("Threshold", "Search", new XAttribute("Max", 100)),
    					  new XElement("Threshold", "Search", new XAttribute("Min", "20")),
    					  new XElement("ROI", "Rect2"),
    					  new XElement("Date", DateTime.Now.ToString("yyyy-MM-dd"))
    					   )));
    			string iniPath = Application.StartupPath + "\params";
    			string xmlPath = iniPath + "\ProductInfo.xml";
    			//需要指定编码格式,否则在读取时会抛:根级别上的数据无效。 第 1 行 位置 1异常
    			XmlWriterSettings settings = new XmlWriterSettings();
    		        settings.Encoding = new UTF8Encoding(false);
    		        settings.Indent = true;
    		        XmlWriter xw = XmlWriter.Create(xmlPath, settings);//不用xmlwriter 用document写也一样的,但是writer快一点
    		        xElement.Save(xw);
                            xw.Flush();
    		        xw.Close();
    
    3. 使用Xdocument建立(树结构,速度比writer慢,不推荐)
    点击查看代码
     
              //使用XDocument创建xml
                 System.Xml.Linq.XDocument xdoc = new XDocument();
                 XDeclaration xdec = new XDeclaration("1.0", "utf-8", "yes");
                 xdoc.Declaration = xdec;
      
                  //添加根节点
                 XElement rootEle = new XElement("school");
                 xdoc.Add(rootEle);
     
                 //给根节点添加子节点
                 XElement classEle = new XElement("class");
                 XAttribute attrClass = new XAttribute("No", 1);
                 classEle.Add(attrClass);
                 rootEle.Add(classEle);//其实就是看哪个Xelement.Add的,哪个加的哪个就是谁的子节点
    
                 XElement classEle1 = new XElement("class1");
                 XAttribute attrClass1 = new XAttribute("No", 1);
                 classEle1.Add(attrClass1);
                 rootEle.Add(classEle1);
    
                 //添加子节点下的元素
                 XElement stuEle = new XElement("student");
                 XAttribute atrStu = new XAttribute("sid", "20180101");
                 stuEle.Add(atrStu);
                 classEle.Add(stuEle);
    
                 //保存文件
                 xdoc.Save("d:\zzz\TestB.xml");
    
    
    二.Xml的读取 1.使用Xdocument进行读取 var doc =Xdocument.Load( 路径【string】 ); 或者Xdocument doc=Xdocument.Load( 路径【string】 );

    XDocument 属性
    三.Xml的增加
    1.可以用上面第三种里面的XElement.Add()去添加节点到对应的位置
    //没写完

  • 相关阅读:
    frp 多个web服务内网映射外网IP
    vue 组件和路由 cdn引入方式的写法。。。。
    Http/Https面试题整理+三次握手四次挥手
    推荐系统的工程实现
    Testner读书会暨公益图书馆项目正式启动
    学习算法
    工作流开源任务调度框架2--Azkaban
    工作流开源调度框架1- airflow
    Testner软件质量与测试创新研究中心总部落地长沙
    企业的八大类别36种盈利模式
  • 原文地址:https://www.cnblogs.com/dengzhekaihua/p/15465946.html
Copyright © 2011-2022 走看看