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

来源:互联网 发布:金融公司网络销售面试 编辑:程序博客网 时间:2024/05/29 18:16
1 例如界面有很多个控件,先用div框起来。    

    <div id="Div_BearingStructure2" runat="server">            <ul>                <li>                    <smwc:CheckBoxEx ID="House_Incline" runat="server" Text="房屋明显倾斜"></smwc:CheckBoxEx>                    <smwc:TextBoxEx ID="Inclination" runat="server" CaptionText="(大于 " Width="150"> </smwc:TextBoxEx>                    <strong>方  向:   </strong>                    <asp:CheckBoxList ID="Incline_Direction" runat="server" RepeatDirection="Horizontal">                        <asp:ListItem Text="纵向" Value="1" onclick="CheckBoxList_Click(this)"></asp:ListItem>                        <asp:ListItem Text="横向" Value="2" onclick="CheckBoxList_Click(this)"></asp:ListItem>                   </asp:CheckBoxList>                </li>                <li>                    <smwc:CheckBoxEx ID="Building_Examine3" runat="server" Text="柱根有水平裂缝"></smwc:CheckBoxEx>                    <smwc:TextBoxEx ID="Building_Location3" runat="server" CaptionText="位置:" Width="150"> </smwc:TextBoxEx>                </li>                <li>                    <smwc:CheckBoxEx ID="Building_Examine4" runat="server" Text="承重墙明显开裂(竖向、交叉)"></smwc:CheckBoxEx>                                                 <smwc:TextBoxEx ID="Building_Location4" runat="server" CaptionText="位置:" Width="150"> </smwc:TextBoxEx>                                                 <smwc:TextBoxEx ID="Building_Legth1" runat="server" CaptionText="长度:" Width="150"> </smwc:TextBoxEx>                </li>            <ul></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));<span style="white-space:pre"></span>   <span style="white-space:pre"></span>//创建一个XML, 在指定的节点插入自己的子节点 _XmlNod指的是子节点            _SubXmlElement = _XmlDocument.CreateElement(_XmlNod);<span style="white-space:pre"></span> <span style="white-space:pre"></span>//循环 Div_BearingStructure2 这个div             foreach (Control control in Div_BearingStructure2.Controls)            {<span style="white-space:pre"></span>//按类型查找   看是否有下面这些控件                if (control.GetType() == typeof(CheckBoxEx) || control.GetType() == typeof(TextBoxEx) || control.GetType() == typeof(CheckBoxList))                 {<span style="white-space:pre"></span>//转换为具体控件类型                       CheckBoxEx _CheckBoxEx = control as CheckBoxEx;                      TextBoxEx _TextBoxEx = control as TextBoxEx;                    CheckBoxList _CheckBoxList = control as CheckBoxList;                    if (_CheckBoxEx != null)                    {
<pre name="code" class="html"><span style="white-space:pre"></span>//选择框是否有被选择,把名字控件名称和内容写入 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>(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
<span style="white-space:pre"></span>//_GetCheckinfo       #region 读取XMl,回写到界面        private void SetBearingStructureValue2Control()        {            string BearingStructure2 = _ProInst.SecurityCheck.BearingStructure2.ToString();<span style="white-space:pre"></span>  //获取数据库的XMl            if (!string.IsNullOrEmpty(BearingStructure2))            {                XmlDocument _XmlDocument = new XmlDocument();                _XmlDocument.LoadXml(BearingStructure2);                XmlNode xmlNode = _XmlDocument.SelectSingleNode("/SuperMap/BearingStructure2");<span style="white-space:pre"></span>//读取指定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)) //按类型查找                              {  <span style="white-space:pre"></span># 先循环 控件 更新控件                                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;                                            }                                        }                                    }                                }                            }                        }                                       }                }            }           }

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

0 0
原创粉丝点击