-- 数据库表的创建
IF OBJECT_ID('Sys_File') IS NOT NULL
DROP TABLE Sys_FileGO
CREATE TABLE Sys_File
(
Id INT IDENTITY(1,1) PRIMARY KEY -- 主键Id
,ParentId INT -- 父文件Id
,SysFileName NVARCHAR(100) -- 文件名称
,IsEnable INT DEFAULT 0 -- 是否可用
)
GO
---读取xml文件的方法-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// 读取XML文件
/// </summary>
/// <param name="filepath"></param>
private void ReadXML(string filepath)
{
XmlDocument doc = new XmlDocument();
doc.Load(filepath); //加载Xml文件
XmlElement rootElem = doc.DocumentElement; //获取根节点
_TreeOperation.AddSysFile(ParentId,rootElem.Name); // 插入根节点数据
#region 获取ParentId
DataTable dt = _TreeOperation.GetSysFile(rootElem.Name);
if (null != dt && dt.Rows.Count > 0)
{
ParentId = Convert.ToInt32(dt.Rows[0]["Id"]);
}
#endregion 获取ParentId
foreach (XmlNode xl in rootElem.ChildNodes) //获取book子节点集合
{
ReadXmlNode1(xl,ParentId);
}
}
---方法一 foreach 实现------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// 读取节点, 直到最底层的数据
/// </summary>
/// <param name="xl"></param>
/// <returns></returns>
private void ReadXmlNode1(XmlNode xl,int i)
{
XmlNode xNode; //存放xml节点
XmlNodeList xNodeList; //存放节点集合(这里是所有子节点的集合)
if (xl.HasChildNodes) // 若有子节点则,进行循环插入
{
_TreeOperation.AddSysFile(i,xl.Name);
int Pid = _TreeOperation.GetMaxId(); // 获取刚新增的数据Id
xNodeList = xl.ChildNodes;
foreach (XmlNode item in xNodeList)
{
xNode = item;
ReadXmlNode1(xNode,Pid);
}
}
else
{
_TreeOperation.AddSysFile(i,xl.Name);
}
}
------方法二:for循环 实现---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// 读取XML文件
/// </summary>
/// <param name="filepath"></param>
private void ReadXML(string filepath)
{
XmlDocument doc = new XmlDocument();
doc.Load(filepath); //加载Xml文件
XmlElement rootElem = doc.DocumentElement; //获取根节点
_TreeOperation.AddSysFile(ParentId,ParentId);
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
相关网址:http://bbs.csdn.net/topics/370042070