將TreeView中匯出為XML資料

來源:互聯網
上載者:User

//使用範例:
TreeViewToXmlB m_TreeViewToXmlB = new TreeViewToXmlB();
 m_TreeViewToXmlB.exportToXml(this.TreeView1,Server.MapPath(@".\xml.xml"));

====================================================

using System;
using Microsoft.Web.UI.WebControls;
using System.Collections;
using System.IO;
//using System.Windows.Forms;

namespace treeview_DB
{
 /// <summary>
 /// 將TreeView中匯出為XML資料
 /// </summary>
 public class TreeViewToXmlB
 {
  //XML每行的內容
  private string xmlLine = "";
  //用於寫XML檔案
  private StreamWriter sr;

  //// <summary>
  /// 建構函式
  /// </summary>
  public TreeViewToXmlB()
  {
           
  }

  /// <summary>
  /// 將TreeView中匯出為XML資料
  /// </summary>
  /// <param name="tv">TreeView控制項</param>
  /// <param name="filename">XML檔案的儲存路徑</param>
  public void exportToXml(TreeView tv, string filename)
  {
   sr = new StreamWriter(filename, false, System.Text.Encoding.UTF8);
   //寫檔案頭部內容
   //下面是產生RSS的OPML檔案
   sr.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
   sr.WriteLine("<opml version=\"1.0\">");
   sr.WriteLine("<head>");
   sr.WriteLine("<title>RSS</title>");
   sr.WriteLine("</head>");
   sr.WriteLine("<body>");

   //遍曆根節點
   foreach (TreeNode node in tv.Nodes)
   {
    if (node.Nodes.Count == 0)
    {
     xmlLine = GetRSSText(node);
    }
    else
    {
     xmlLine = GetDirectoryText(node);
    }

    sr.WriteLine(xmlLine);

    //遞迴遍曆節點
    parseNode(node);
   }

   //寫檔案尾部內容
   sr.WriteLine("</body>");
   sr.WriteLine("</opml>");
   sr.Close();
  }

  //遞迴遍曆節點內容,最關鍵的函數
  private void parseNode(TreeNode tn)
  {
   IEnumerator ie = tn.Nodes.GetEnumerator();

   while (ie.MoveNext())
   {
    TreeNode ctn = (TreeNode) ie.Current;

    //為最底層節點,即該節點為RSS節點
    if (ctn.Nodes.Count == 0)
    {
     xmlLine = GetRSSText(ctn);
     sr.WriteLine(xmlLine);
    }
    else//不是最底層節點,即該節點為目錄節點
    {
     xmlLine = GetDirectoryText(ctn);
     sr.WriteLine(xmlLine);
    }

    //如果還有子節點則繼續遍曆
    if (ctn.Nodes.Count > 0)
    {
     parseNode(ctn);
    }
   }

   sr.WriteLine("</outline>");
  }

  //成生RSS節點的XML文本行
  private string GetRSSText(TreeNode node)
  {
   //根據Node屬性產生XML文本
   string rssText = "<outline type=\"rss\" text=\"" + node.Text + "\" xmlUrl=\"URL\"/>";

   return rssText;
  }

  //成生目錄節點的XML文本行
  private string GetDirectoryText(TreeNode node)
  {
   //根據Node屬性產生XML文本
   string directoryText = "<outline text=\"" + node.Text + "\">";

   return directoryText;
  }
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.