从XML文件读取数据绑定到列表控件2

 ComponentArt.Web.UI控件绑定所用XML,同时用于DropDownList的绑定,XML如下:
< SiteMap >
  
< item  Text ="标题一"  Id ="1"   />
  
< item  Text ="标题二" Id ="4"   />
  
< item  Text ="标题三" Id ="5" />
  
< item  Text ="标题四"  Id ="6" />
  
< item  Text ="标题五"  Id ="7"   />
</ SiteMap >
         C#代码:
 1           ///   <summary>
 2           ///  从XML文件读取数据绑定到列表控件
 3           ///   </summary>
 4           ///   <example>
 5           ///   <code>
 6           ///  ReadXMLBindListControl("tabData.xml", this.Ddl1);
 7           ///   </code>
 8           ///   </example>
 9           ///   <param name="filepath"> XML文件路径 </param>
10           ///   <param name="lic"> ListControl ID </param>
11           public   static   void  ReadXMLBindListControl( string  filepath, System.Web.UI.WebControls.ListControl lic)
12          {
13               string  path  =  System.Web.HttpContext.Current.Server.MapPath(filepath);
14               if  ( ! File.Exists(path))
15              {
16                   throw   new  FileNotFoundException( " Specified XmlFile ( "   +  filepath  +   " ) does not exist. " );
17              }
18 
19              lic.Items.Clear();
20              XmlDocument doc  =   new  XmlDocument();
21              doc.Load(path);
22               foreach  (XmlNode node  in  doc.DocumentElement.ChildNodes)
23              {
24                   if  (node.NodeType  ==  XmlNodeType.Element)
25                  {
26                      lic.Items.Add( new  ListItem(node.Attributes[ 0 ].Value, node.Attributes[ 1 ].Value));
27                  }
28              }
29          }

转载于:https://www.cnblogs.com/hzuIT/articles/900514.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值