这个是一个例子生成Xml文件的例子BuildXmlFile.cs ___________________________ namespace wltmis.Xml { using System; using System.Data; using System.Xml; using System.Text; using System.Data.OleDb; /// <summary> /// BuildXmlFile 的摘要说明。 /// 创建权限xml文档,提供系统用户登陆系统的权限设定文件 /// </summary> public class BuildXmlFile { public static OleDbConnection myConnection; public BuildXmlFile() { }
public void initree(string roleid,string argsPath) { myConnection = new OleDbConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]); myConnection.Open(); int layer =0; XmlTextWriter writer = null; try { writer = new XmlTextWriter(argsPath,null); writer.Formatting = Formatting.Indented; writer.WriteStartDocument(true); writer.WriteComment("XML nodes added using the xmltextwriter"); writer.WriteStartElement("TREENODES"); writer.WriteAttributeString("xmlns","x-schema:MenuRight.xdr"); writer = Tree(layer,"0",roleid,writer); writer.WriteEndElement(); //向文件写 XML 并关闭编写器 writer.Flush(); writer.Close(); /* XmlDocument doc = new XmlDocument(); doc.Load(xmlDoc); Response.ContentType = "text/xml"; doc.Save(Response.Output); */ } catch (Exception e) { Console.WriteLine ("异常:{0}", e.ToString()); } finally { myConnection.Close(); if (writer != null) { writer.Close(); } } } private XmlTextWriter Tree(int layer,string tid,string roleid,XmlTextWriter XmlWriter) { String sqlstr = "select a.roleid,a.funid,b.funname,b.funlink,b.far_id from tb_rolefunc a,tb_func b where a.funid=b.funid and a.roleid="+roleid+" and b.far_id="+tid; OleDbDataAdapter myCommand = new OleDbDataAdapter(sqlstr, myConnection); DataSet ds = new DataSet(); myCommand.Fill(ds, "tb_rolefunc"); DataView dv = ds.Tables["tb_rolefunc"].DefaultView; foreach(DataRowView drv in dv ) { XmlWriter.WriteStartElement("TreeNode", null); XmlWriter.WriteAttributeString("TEXT",drv.Row[2].ToString()); XmlWriter.WriteAttributeString("NavigateURL",drv.Row[3].ToString()); XmlWriter.WriteAttributeString("Target","_self"); Tree(layer+1,drv.Row["funid"].ToString(),roleid,XmlWriter); //递归调用 XmlWriter.WriteEndElement(); } return XmlWriter; } } }调用 using wltmis.Xml;private void BuildXml(string rid) { String xmlDoc = Server.MapPath("roleXml/role"+q_roleid+".xml"); BuildXmlFile RoleXml = new BuildXmlFile(); RoleXml.initree(rid,xmlDoc); }
1。如果数据量不大的话,你可以直接生成menu.xml不要用数据库来保存菜单数据了。以后更改菜单就直接对xml来操作。完全忽略数据库。
2。如果需要数据库,那也可以以后动态从数据库中取数据,动态生成xml。
请问这个打开页面用一个新窗口来打开的话,要怎么写?
你用的是什么菜单控件,应该有traget的属性
___________________________
namespace wltmis.Xml
{ using System;
using System.Data;
using System.Xml;
using System.Text;
using System.Data.OleDb;
/// <summary>
/// BuildXmlFile 的摘要说明。
/// 创建权限xml文档,提供系统用户登陆系统的权限设定文件
/// </summary>
public class BuildXmlFile
{
public static OleDbConnection myConnection; public BuildXmlFile()
{
}
public void initree(string roleid,string argsPath)
{
myConnection = new OleDbConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
myConnection.Open();
int layer =0;
XmlTextWriter writer = null;
try
{
writer = new XmlTextWriter(argsPath,null);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument(true);
writer.WriteComment("XML nodes added using the xmltextwriter");
writer.WriteStartElement("TREENODES");
writer.WriteAttributeString("xmlns","x-schema:MenuRight.xdr");
writer = Tree(layer,"0",roleid,writer);
writer.WriteEndElement(); //向文件写 XML 并关闭编写器
writer.Flush();
writer.Close();
/*
XmlDocument doc = new XmlDocument();
doc.Load(xmlDoc);
Response.ContentType = "text/xml";
doc.Save(Response.Output);
*/
}
catch (Exception e)
{
Console.WriteLine ("异常:{0}", e.ToString());
}
finally
{
myConnection.Close();
if (writer != null)
{
writer.Close();
}
}
}
private XmlTextWriter Tree(int layer,string tid,string roleid,XmlTextWriter XmlWriter)
{
String sqlstr = "select a.roleid,a.funid,b.funname,b.funlink,b.far_id from tb_rolefunc a,tb_func b where a.funid=b.funid and a.roleid="+roleid+" and b.far_id="+tid;
OleDbDataAdapter myCommand = new OleDbDataAdapter(sqlstr, myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "tb_rolefunc");
DataView dv = ds.Tables["tb_rolefunc"].DefaultView;
foreach(DataRowView drv in dv )
{
XmlWriter.WriteStartElement("TreeNode", null);
XmlWriter.WriteAttributeString("TEXT",drv.Row[2].ToString());
XmlWriter.WriteAttributeString("NavigateURL",drv.Row[3].ToString());
XmlWriter.WriteAttributeString("Target","_self");
Tree(layer+1,drv.Row["funid"].ToString(),roleid,XmlWriter); //递归调用
XmlWriter.WriteEndElement();
}
return XmlWriter;
}
}
}调用
using wltmis.Xml;private void BuildXml(string rid)
{
String xmlDoc = Server.MapPath("roleXml/role"+q_roleid+".xml");
BuildXmlFile RoleXml = new BuildXmlFile();
RoleXml.initree(rid,xmlDoc);
}