c# 循环控件 把控件的名称和值 写入xml中

1 例如界面有很多个控件,先用div框起来。    

 

   
  • 方  向:
  •                    

2  C#代码部分 主要是遍历control这种写法
    private string GetCheckinfoToXml(string _GetCheckinfo, string _XmlNod ,System.Web.UI.HtmlControls.HtmlGenericControl _Control)
        {
            XmlElement _XmlElement;
            XmlElement _SubXmlElement;
            XmlDocument _XmlDocument = XmlHelper.CreateDocumentSecurityCheck(XmlEmun._XsltPath(StateOwnedPrintTypeEnum.SecurityCheckInfoName));
	   	//创建一个XML, 在指定的节点插入自己的子节点 _XmlNod指的是子节点
            _SubXmlElement = _XmlDocument.CreateElement(_XmlNod);
	 	//循环 Div_BearingStructure2 这个div 
            foreach (Control control in Div_BearingStructure2.Controls)
            {
			//按类型查找   看是否有下面这些控件
                if (control.GetType() == typeof(CheckBoxEx) || control.GetType() == typeof(TextBoxEx) || control.GetType() == typeof(CheckBoxList)) 
                {
			//转换为具体控件类型   
                    CheckBoxEx _CheckBoxEx = control as CheckBoxEx;  
                    TextBoxEx _TextBoxEx = control as TextBoxEx;
                    CheckBoxList _CheckBoxList = control as CheckBoxList;
                    if (_CheckBoxEx != null)
                    {
			//选择框是否有被选择,把名字控件名称和内容写入 XML 
_XmlElement = _XmlDocument.CreateElement(_CheckBoxEx.ID.ToString()); _SubXmlElement.AppendChild(_XmlElement); if (_CheckBoxEx.Checked == true) { _XmlElement.InnerText = "1"; } else { _XmlElement.InnerText = "0"; } } if (_TextBoxEx != null) { _XmlElement = _XmlDocument.CreateElement(_TextBoxEx.ID.ToString()); _SubXmlElement.AppendChild(_XmlElement); if (_TextBoxEx.Text != null) { _XmlElement.InnerText = _TextBoxEx.Text; } else { _XmlElement.InnerText = ""; } } if (_CheckBoxList != null) { _XmlElement = _XmlDocument.CreateElement(_CheckBoxList.ID.ToString()); _SubXmlElement.AppendChild(_XmlElement); foreach (ListItem _Item in Incline_Direction.Items) { if (_Item.Selected) { _XmlElement.InnerText = _Item.Value;//纵向1 横向2 } else { _XmlElement.InnerText = ""; } } } } } _XmlDocument.DocumentElement.AppendChild(_SubXmlElement); XmlView.TransformSource = XmlEmun._XsltPath(StateOwnedPrintTypeEnum.SecurityCheckInfoName); SecurityCheckInfoName = EnumHelper.GetName(StateOwnedPrintTypeEnum.SecurityCheckInfoName.ToInt32()) + ".xml"; string _TempFileName = XmlEmun._SuperviseStateOwnedTempPath + SecurityCheckInfoName; if (File.Exists(_TempFileName)) File.Delete(_TempFileName); _XmlDocument.Save(_TempFileName); XmlView.DocumentContent = _XmlDocument.InnerXml; _GetCheckinfo = _XmlDocument.InnerXml.ToString(); return _GetCheckinfo; //返回 string类型的XML内容 }
从数据库中取出 
   XML内容  再写入xml 
  
	//_GetCheckinfo
       #region 读取XMl,回写到界面
        private void SetBearingStructureValue2Control()
        {
            string BearingStructure2 = _ProInst.SecurityCheck.BearingStructure2.ToString();
	  //获取数据库的XMl
            if (!string.IsNullOrEmpty(BearingStructure2))
            {
                XmlDocument _XmlDocument = new XmlDocument();
                _XmlDocument.LoadXml(BearingStructure2);
                XmlNode xmlNode = _XmlDocument.SelectSingleNode("/SuperMap/BearingStructure2");
		//读取指定XMl节点
                if (xmlNode != null)
                {
                    foreach (XmlNode node in xmlNode.ChildNodes)
                    {
                        #region 匹配后更新控件 先循环 XML的节点
                        foreach (Control control in Div_BearingStructure2.Controls)
                        {
                            if (control.GetType() == typeof(CheckBoxEx) || control.GetType() == typeof(TextBoxEx)||control.GetType() == typeof(CheckBoxList)) //按类型查找  
                            {
  				# 先循环 控件 更新控件
                                CheckBoxEx _CheckBoxEx = control as CheckBoxEx;  //转换为具体控件类型  
                                TextBoxEx _TextBoxEx = control as TextBoxEx;

                                CheckBoxList _CheckBoxList = control as CheckBoxList;
                                if (_CheckBoxEx != null)
                                {
                                    if (node.Name.ToString() == _CheckBoxEx.ID.ToString())
                                    {
                                        //存在此节点则更新控件显示
                                        if (node.InnerXml.ToString() == "1")
                                        {
                                            _CheckBoxEx.Checked = true;
                                        }
                                        else
                                        {
                                            _CheckBoxEx.Checked = false;
                                        }
                                    }

                                }

                                if (_TextBoxEx != null)
                                {
                                    if (node.Name.ToString() == _TextBoxEx.ID.ToString())
                                    {
                                        //存在此节点则更新控件显示
                                        if (node.InnerXml.ToString() != null)
                                        {
                                            _TextBoxEx.Text = node.InnerXml.ToString();
                                        }
                                        else
                                        {
                                            _TextBoxEx.Text = null;
                                        }
                                    }
                                }
                                if (_CheckBoxList != null)
                                {
                                    if (node.Name.ToString() == _CheckBoxList.ID.ToString())
                                    {
                                        //存在此节点则更新控件显示
                                        foreach (ListItem _Item in Incline_Direction.Items)
                                        {
                                            if (node.InnerXml.ToString() == _Item.Value)
                                            {
                                                _Item.Selected = true;
                                            }
                                        }
                                    }
                                }

                            }
                        }
                   
                    }
                }
            }
   
        }


重点在于如何存和取的过程!

你可能感兴趣的:(数据库,c#,控件,界面)