利用TreeView控件动态生成无限级树(续:通过绑定动态xml文件)
在上一篇
利用TreeView控件动态生成无限级树
中提到的那个动态生成XML文件进行绑定的办法
今天也被我实现了
把生成XML文件的代码单独放到一个页面中,比如说叫GetXml.aspx页中去
然后指定
TreeView2.TreeNodeSrc = "GetXml.aspx";
而这个GetXml.aspx.cs中的代码如下
private void SendXml()
{
//Create Xml File
XmlDocument objXmlDoc = new XmlDocument();
![](/Images/OutliningIndicators/InBlock.gif)
//Insert Xml Declaration
XmlDeclaration objXmlDeclare = objXmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
objXmlDoc.InsertBefore(objXmlDeclare, objXmlDoc.DocumentElement);
![](/Images/OutliningIndicators/InBlock.gif)
XmlElement objRootElem = objXmlDoc.CreateElement("TREENODES");
objXmlDoc.AppendChild(objRootElem);
![](/Images/OutliningIndicators/InBlock.gif)
CreateXml(objXmlDoc, objRootElem, 0);
![](/Images/OutliningIndicators/InBlock.gif)
System.Xml.XmlTextWriter xmlwriter = new System.Xml.XmlTextWriter(Response.OutputStream,Response.ContentEncoding);
xmlwriter.Formatting = Formatting.Indented;
xmlwriter.Indentation = 4;
xmlwriter.IndentChar = ' ';
objXmlDoc.WriteTo(xmlwriter);
xmlwriter.Flush();
Response.End();
xmlwriter.Close();
}
![](/Images/OutliningIndicators/None.gif)
private void CreateXml(XmlDocument objXMLDoc, XmlElement objRootElem, int belong)
{
//Get DataSet
DataSet ds = new DataSet();
foreach(DataRow dr in ds.Tables[0].Rows)
{
//Create ChildNode TreeNode
XmlElement objXmlElem = objXMLDoc.CreateElement("TREENODE");
objRootElem.AppendChild(objXmlElem);
![](/Images/OutliningIndicators/InBlock.gif)
//Create Attributes Text
XmlAttribute objXmlAttText = objXMLDoc.CreateAttribute("Text");
objXmlAttText.Value = dr["text"].ToString().Trim();
objXmlElem.SetAttributeNode(objXmlAttText);
![](/Images/OutliningIndicators/InBlock.gif)
//Create Attributes CheckBox
XmlAttribute objXmlAttCB = objXMLDoc.CreateAttribute("CheckBox");
objXmlAttCB.Value = "True";
objXmlElem.SetAttributeNode(objXmlAttCB);
![](/Images/OutliningIndicators/InBlock.gif)
//Create Attributes Expanded
XmlAttribute objXmlAttExp = objXMLDoc.CreateAttribute("Expanded");
objXmlAttExp.Value = "True";
objXmlElem.SetAttributeNode(objXmlAttExp);
![](/Images/OutliningIndicators/InBlock.gif)
int id = int.Parse(dr["id"].ToString().Trim());
CreateXml(objXMLDoc, objXmlElem, id);
}
}
函数的递归思想用的仍然是上一篇中的同样的思想
这里把生成的objXMLdoc文件用XmlTextWriter写入到一个Xml流中
然后利用xmlwriter.Flush()的方式进行输出
这样就可以得到这个Xml文件了
在PageLoad函数里调用这个SendXml()方法即可
private void Page_Load(object sender, System.EventArgs e)
{
BindXmlTree();
}
利用TreeView控件动态生成无限级树
中提到的那个动态生成XML文件进行绑定的办法
今天也被我实现了
把生成XML文件的代码单独放到一个页面中,比如说叫GetXml.aspx页中去
然后指定
![](/Images/OutliningIndicators/None.gif)
而这个GetXml.aspx.cs中的代码如下
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
函数的递归思想用的仍然是上一篇中的同样的思想
这里把生成的objXMLdoc文件用XmlTextWriter写入到一个Xml流中
然后利用xmlwriter.Flush()的方式进行输出
这样就可以得到这个Xml文件了
在PageLoad函数里调用这个SendXml()方法即可
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)