kenlistian

勤学多思

  IT博客 :: 首页 :: 新随笔 ::  :: 聚合  :: 管理 ::
  412 随笔 :: 0 文章 :: 23 评论 :: 0 Trackbacks

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

using System.Xml;
using System.Text;
using System.IO;


/// <summary>
/// 改自http://www.codeproject.com/cs/miscctrl/treeviewxml.asp
/// 原作者:UsualDosage.
/// 改: kenlistian
/// </summary>

namespace xml2tree
{

public class Xml2TreeList
{
private XmlTextWriter xr;
string xmlfile;

public Xml2TreeList(string filename)
{
xmlfile = filename;
if (xmlfile == "")
{
xmlfile = Application.ProductName + ".xml";
}
}

//写到xml文件中
public void ToXml(TreeView tv,string sFileName)
{
if (sFileName == "")
sFileName = xmlfile;
xr = new XmlTextWriter(sFileName, System.Text.Encoding.UTF8);
xr.WriteStartDocument();
xr.WriteStartElement(tv.Nodes[0].Text);
foreach (TreeNode node in tv.Nodes)
{
saveNode2(node.Nodes);
}

xr.WriteEndElement();
xr.Close();
}

//从xml文件中读出到treeview中
// sFileName 读取的文件名
// TreeView 传入的控件树
public bool ToTreeView(TreeView tv, string sFileName)
{
if (sFileName == "")
sFileName = xmlfile;
try
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(sFileName);
tv.Nodes.Clear();

tv.Nodes.Add(new TreeNode(xDoc.DocumentElement.Name));

TreeNode tNode = new TreeNode();
tNode = (TreeNode)tv.Nodes[0];

addTreeNode(xDoc.DocumentElement, tNode);
tv.ExpandAll();
}
catch (XmlException xExc)
{
// sMsg = xExc.Message;
return false;
}
catch (Exception ex)
{
//sMsg = ex.Message
return false;

}
finally
{

}
return true;
}









//
private void saveNode2(TreeNodeCollection tnc)
{
foreach (TreeNode node in tnc)
{
if (node.Nodes.Count > 0)
{
xr.WriteStartElement(node.Text);
saveNode2(node.Nodes);
xr.WriteEndElement();
}
else
{
xr.WriteString(node.Text);
}
}
}
//
private void addTreeNode(XmlNode xmlNode, TreeNode treeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList xNodeList;
if (xmlNode.HasChildNodes)
{
xNodeList = xmlNode.ChildNodes;
for (int x = 0; x <= xNodeList.Count - 1; x++)
{
xNode = xmlNode.ChildNodes[x];
treeNode.Nodes.Add(new TreeNode(xNode.Name));
tNode = treeNode.Nodes[x];
addTreeNode(xNode, tNode);
}
}
else
treeNode.Text = xmlNode.OuterXml.Trim();
}





}



/* 采用XmlTextWriter方法是一种写xml文件的方法
* 采用StreamWriter 方法也是一种xml文件的方法

private StreamWriter sr;

public void exportToXml(TreeView tv, string filename)
{
sr = new StreamWriter(filename, false, System.Text.Encoding.UTF8);
//Write the header
sr.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
//Write our root node
sr.WriteLine("<" + treeView1.Nodes[0].Text + ">");
foreach (TreeNode node in tv.Nodes)
{
saveNode(node.Nodes);
}
//Close the root node
sr.WriteLine("</" + treeView1.Nodes[0].Text + ">");
sr.Close();
}

private void saveNode(TreeNodeCollection tnc)
{
foreach (TreeNode node in tnc)
{
//If we have child nodes, we'll write
//a parent node, then iterrate through
//the children
if (node.Nodes.Count > 0)
{
sr.WriteLine("<" + node.Text + ">");
saveNode(node.Nodes);
sr.WriteLine("</" + node.Text + ">");
}
else //No child nodes, so we just write the text
sr.WriteLine(node.Text);
}
}
*/

}

posted on 2007-08-03 13:56 kenlistian 阅读(578) 评论(1)  编辑 收藏 引用
只有注册用户登录后才能发表评论。