C#使用System.xml.linq來產生XML檔案

來源:互聯網
上載者:User

標籤:

直接看代碼:

  /*              * <?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");

  

C#使用System.xml.linq來產生XML檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.