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");
            }
  • 相关阅读:
    Linux autojump命令
    Linux rpmbuild命令
    RPM包制作教程
    LSP(分层服务提供程序)
    WPAD 的原理及实现
    在OpenSSL中添加自定义加密算法
    Openssl sess_id命令
    hdu1878欧拉回路(DFS+欧拉回路)
    好代码的伪科学定义
    MySQL安装过程中出现“APPLY security settings错误”的解决方式
  • 原文地址:https://www.cnblogs.com/xingshikk/p/2616163.html
Copyright © 2011-2022 走看看