zoukankan      html  css  js  c++  java
  • Linq To Xml 创建修改xml文档

     static void Main(string[] args)
            {
                //创建xml文件
                string path = @"E:\LinqToXmlTest.xml";
                XDocument xdoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
                new XElement("Root",
                    new XElement("AccessCount",
                        new XAttribute("ClientId", 123),
                        new XAttribute("DateTime", DateTime.Now.ToString()))
                    ));
                xdoc.Save(path);



                //读取xml文档
                XElement root = XElement.Load(@"E:\LinqToXmlTest.xml");

                //修改xml文档
                var clientinfo = from h in root.Descendants("AccessCount")
                                 where h.Attribute("ClientId").Value == "123"
                                 select h;
                var r = clientinfo.Single<XElement>();
                r.ReplaceWith(new XElement("AccessCount",
                     new XAttribute("ClientId", 123456789),
                       new XAttribute("DateTime", DateTime.Now.ToString())));


                //最后保存文件
                root.Save(@"E:\LinqToXmlTest.xml");
            }
  • 相关阅读:
    vue ssr
    webpack-dev-server proxy代理
    PHP连数据库生成数据字典
    redis.rpm 安装
    centos 6.5安装NodeJS
    Jenkins + git + maven 安装
    最新版本GIT安装
    身份证校验
    快递100物流公司列表
    redis 安装
  • 原文地址:https://www.cnblogs.com/xingshikk/p/2616163.html
Copyright © 2011-2022 走看看