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

来源:互联网 发布:知乎如何添加图片 编辑:程序博客网 时间:2024/06/11 10:24
   界面 有某个UI  先用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>           //还有N个            <ul></div>

2 C#

   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>//创建一个XML, 在指定的节点插入自己的子节点 _XmlNod指的是子节点            _SubXmlElement = _XmlDocument.CreateElement(_XmlNod);<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)                    {                        _XmlElement = _XmlDocument.CreateElement(_CheckBoxEx.ID.ToString());                        _SubXmlElement.AppendChild(_XmlElement);<span style="white-space:pre"></span>//选择框是否有被选择,把名字控件名称和内容写入 XML                         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;<span style="white-space:pre"></span>//返回 string类型的XML内容         }
<span style="white-space:pre"></span>从数据库中取出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>//按类型查找                              {  <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;                                            }                                        }                                    }                                }                            }                        }                        #endregion                    }                }            }           }        #endregion


0 0