利用TreeView控件动态生成无限级树(续:通过绑定动态xml文件)(转)

在上一篇
利用TreeView控件动态生成无限级树
中提到的那个动态生成XML文件进行绑定的办法
今天也被我实现了

把生成XML文件的代码单独放到一个页面中,比如说叫GetXml.aspx页中去
然后指定
None.gif TreeView2.TreeNodeSrc  =   " GetXml.aspx " ;

而这个GetXml.aspx.cs中的代码如下
None.gif private   void  SendXml()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
//Create Xml File
InBlock.gif
            XmlDocument objXmlDoc = new XmlDocument();
InBlock.gif
InBlock.gif            
//Insert Xml Declaration
InBlock.gif
            XmlDeclaration objXmlDeclare = objXmlDoc.CreateXmlDeclaration("1.0""UTF-8""yes");
InBlock.gif            objXmlDoc.InsertBefore(objXmlDeclare, objXmlDoc.DocumentElement);
InBlock.gif
InBlock.gif            XmlElement objRootElem 
= objXmlDoc.CreateElement("TREENODES");
InBlock.gif            objXmlDoc.AppendChild(objRootElem);
InBlock.gif
InBlock.gif            CreateXml(objXmlDoc, objRootElem, 
0);
InBlock.gif
InBlock.gif            System.Xml.XmlTextWriter xmlwriter 
= new System.Xml.XmlTextWriter(Response.OutputStream,Response.ContentEncoding);
InBlock.gif            xmlwriter.Formatting 
= Formatting.Indented;
InBlock.gif            xmlwriter.Indentation 
= 4;
InBlock.gif            xmlwriter.IndentChar 
= ' ';
InBlock.gif            objXmlDoc.WriteTo(xmlwriter);
InBlock.gif            xmlwriter.Flush();
InBlock.gif            Response.End();
InBlock.gif            xmlwriter.Close();
ExpandedBlockEnd.gif        }

None.gif
None.gif        
private   void  CreateXml(XmlDocument objXMLDoc, XmlElement objRootElem,  int  belong)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
//Get DataSet
InBlock.gif
            DataSet ds = new DataSet();
InBlock.gif            
foreach(DataRow dr in ds.Tables[0].Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//Create ChildNode TreeNode
InBlock.gif
                XmlElement objXmlElem = objXMLDoc.CreateElement("TREENODE");
InBlock.gif                objRootElem.AppendChild(objXmlElem);
InBlock.gif
InBlock.gif                
//Create Attributes Text
InBlock.gif
                XmlAttribute objXmlAttText = objXMLDoc.CreateAttribute("Text");
InBlock.gif                objXmlAttText.Value 
= dr["text"].ToString().Trim();
InBlock.gif                objXmlElem.SetAttributeNode(objXmlAttText);
InBlock.gif
InBlock.gif                
//Create Attributes CheckBox
InBlock.gif
                XmlAttribute objXmlAttCB = objXMLDoc.CreateAttribute("CheckBox");
InBlock.gif                objXmlAttCB.Value 
= "True";
InBlock.gif                objXmlElem.SetAttributeNode(objXmlAttCB);
InBlock.gif
InBlock.gif                
//Create Attributes Expanded
InBlock.gif
                XmlAttribute objXmlAttExp = objXMLDoc.CreateAttribute("Expanded");
InBlock.gif                objXmlAttExp.Value 
= "True";
InBlock.gif                objXmlElem.SetAttributeNode(objXmlAttExp);
InBlock.gif
InBlock.gif                
int id = int.Parse(dr["id"].ToString().Trim());
InBlock.gif                CreateXml(objXMLDoc, objXmlElem, id);
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

函数的递归思想用的仍然是上一篇中的同样的思想
这里把生成的objXMLdoc文件用XmlTextWriter写入到一个Xml流中
然后利用xmlwriter.Flush()的方式进行输出
这样就可以得到这个Xml文件了
在PageLoad函数里调用这个SendXml()方法即可
None.gif private   void  Page_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            BindXmlTree();
ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/coolylh/archive/2006/01/07/312767.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值