zoukankan      html  css  js  c++  java
  • C#使用System.xml.linq来生成XML文件

    直接看代码:

      /* 
                 * <?xml version="1.0" encoding="utf-8"?>
                 * <Files Path="123" ExeFile="456">
                 *     <File>
                 *          <LocalName>abc</LocalName>
                 *          <FileSize>abc</FileSize>
                 *      </File>
                 *  </Files>
                 *  
                 * 1. XDocument指的是整个XML文件
                 * 2. XElement指的是每一个节点:如上的:<Files> 、<File>
                 * 3. XAttribut指的是节点的属性,如:<Files Path="123">中的 Path="123"
                 */
    
                //创建根节点
                XElement xFiles = new XElement("Files", new XAttribute("Path", "123"), new XAttribute("ExeFile", "456"));
    
                string[] abc = new string[] { "abc","bcd","efg"};
                foreach (string file in abc)
                {
                    //循环生成子节点
                    XElement xFile = new XElement("File",
                               new XElement("ServerLocation", file),
                               new XElement("LocalLocation", file),
                               new XElement("LocalName", file),
                               new XElement("FileSize", file),
                               new XElement("Sha2Char", file)
                               );
    
                    xFiles.Add(xFile);//只能讲节点加入另一个节点的子节点中,而不能加到XDocument中去
    
                }
                XDocument xdoc = new XDocument(xFiles);//将根节点传入XDocument的构造方法中
                xdoc.Save("e:\123.xml");
    

      

  • 相关阅读:
    随机数模块(random)
    时间模块(time)
    collection模块 1
    collection模块
    re模块
    正则
    Bootstrap 关于下拉菜单的使用
    Bootstrap 关于Glyphicons 字体图标的使用
    oracle拼音排序
    ajax缓存问题
  • 原文地址:https://www.cnblogs.com/tommy-huang/p/4802452.html
Copyright © 2011-2022 走看看