做好设计器后,如何把组件保存到XML中并能回执?就假设,设计好后,有一个窗体,一个button,一个textbox?

解决方案 »

  1.   

    把所有需要的属性都保存到XML中。要用的时候再根据这些属性重建控件!
      

  2.   

    简单的多的是??你要什么效果!!
    XML文件操作
    评论/浏览(0)发表时间:2007年7月2日 10时33分 
     
    [%repeat_0 match="/data/option"%] [%=@title%] [%=@count%]票 [[%=@percent%]%] 
    [%_repeat_0%] 
     
     
      已知有一个XML文件(bookstore.xml)如下:   
      <?xml   version=”1.0”   encoding=”gb2312”?>   
      <bookstore>   
          <book   genre=”fantasy”   ISBN=”2-3631-4>   
      <title>Oberon’s   Leqacy</title>   
      <author>Corets,Eva</author>   
      <pricr>5.95</price>   
          </book>   
      </bookstore>   
      1、 往<bookstore>结点中插入一个<book>结点:   
      Dim   xmlDoc   As   Xml.XmlDocument   =   New   Xml.XmlDocument   '新建一个XML文档   
                      xmlDoc.Load("..\bookstore.xml")   '加载指定的XML   数据   
                      Dim   root   As   Xml.XmlNode   '声明   XML   文档中的单个节点   
                      root   =   xmlDoc.SelectSingleNode("bookstore")   '查找<bookstore>   
                      If   root   Is   Nothing   Then   
                              MessageBox.Show("cuowu")   
                      End   If   
                      Dim   xe1   As   Xml.XmlElement   '声明一个元素   
                      xe1   =   xmlDoc.CreateElement("book")   '创建一个<book>节点   
                      xe1.SetAttribute("genre",   "李赞红")   '设置该节点genre属性   
                      xe1.SetAttribute("ISBN",   "2-3631-4")   '设置该节点ISBN属性   
                      Dim   xesub1   As   Xml.XmlElement   
                      xesub1   =   xmlDoc.CreateElement("title")   
                      xesub1.InnerText   =   "CS从入门到精通"   '设置文本结点   
                      xe1.AppendChild(xesub1)   '添加到<book>结点中   
                      Dim   xesub2   As   Xml.XmlElement   
                      xesub2   =   xmlDoc.CreateElement("author")   
                      xesub2.InnerText   =   "侯捷"   
                      xe1.AppendChild(xesub2)   
                      Dim   xesub3   As   Xml.XmlElement   
                      xesub3   =   xmlDoc.CreateElement("price")   
                      xesub3.InnerText   =   "58.3"   
                      xe1.AppendChild(xesub3)   
                      Try   
                              root.AppendChild(xe1)   '添加到<bookstore>结点中   
                      Catch   ex   As   Exception   
                              MessageBox.Show(ex.Message)   
                      End   Try   
                      xmlDoc.Save("..\bookstore.xml")   
      2、 修改节点:将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点<author>的文本修改为“亚胜”。   
      Dim   xmlDoc   As   Xml.XmlDocument   =   New   Xml.XmlDocument   
                      xmlDoc.Load("..\bookstore.xml")   
                      Dim   nodeList   As   Xml.XmlNodeList   
                      '获取bookstore结点的所有子结点   
                      nodeList   =   xmlDoc.SelectSingleNode("bookstore").ChildNodes   
                      Dim   xn   As   Xml.XmlNode   
                      '遍历所有子结点   
                      For   Each   xn   In   nodeList   
                              Dim   xe   As   Xml.XmlElement   
                              xe   =   xn   '将子节点类型转换为XmlElement类型   
                              If   xe.GetAttribute("genre")   =   "李赞红"   Then   '如果genre属性为“李赞红”   
                                      xe.SetAttribute("genre",   "update李赞红")   '则修改该属性为“update李赞红”   
                                      Dim   nls   As   Xml.XmlNodeList   
                                      nls   =   xe.ChildNodes   '继续获取xe子节点的所有子节点   
                                      Dim   xnl   As   Xml.XmlNode   
                                      For   Each   xnl   In   nls   '遍历   
                                              Dim   xe2   As   Xml.XmlElement   
                                              xe2   =   xnl   '转换类型   
                                              If   xe2.Name   =   "author"   Then   '如果找到   
                                                      xe2.InnerText   =   "亚胜"   '则修改   
                                                      Exit   For   '退出循环   
                                              End   If   
                                      Next   
                                      Exit   For   
                              End   If   
                      Next   
                      xmlDoc.Save("..\bookstore.xml")   '保存    
      

  3.   

    3、 删除   <book   genre="fantasy"   ISBN="2-3631-4">节点的genre属性,删除   <book   genre="update李赞红"   ISBN="2-3631-4">节点。   
      Dim   xmlDoc   As   Xml.XmlDocument   =   New   Xml.XmlDocument   
                      xmlDoc.Load("..\bookstore.xml")   
                      Dim   nodeList   As   Xml.XmlNodeList   
                      '获取bookstore结点的所有子结点   
                      nodeList   =   xmlDoc.SelectSingleNode("bookstore").ChildNodes   
                      Dim   xn   As   Xml.XmlNode   
                      '遍历所有子结点   
                      For   Each   xn   In   nodeList   
                              Dim   xe   As   Xml.XmlElement   
                              xe   =   xn   
                              If   xe.GetAttribute("genre")   =   "fantasy"   Then   
                                      xe.RemoveAttribute("genre")   '删除genre属性   
                              ElseIf   xe.GetAttribute("genre")   =   "update李赞红"   Then   
                                      xe.RemoveAll()   '删除该节点的全部内容   
                              End   If   
                      Next   
                      xmlDoc.Save("..\bookstore.xml")   
      4、 显示所有数据   
      Dim   xmlDoc   As   New   Xml.XmlDocument   
                      xmlDoc.Load("..\bookstore.xml")   
                      Dim   xn   As   Xml.XmlNode   
                      xn   =   xmlDoc.SelectSingleNode("bookstore")   
                      Dim   xnl   As   Xml.XmlNodeList   
                      xnl   =   xn.ChildNodes   
                      Dim   xnf   As   Xml.XmlNode   
                      For   Each   xnf   In   xnl   
                              Dim   xe   As   Xml.XmlElement   
                              xe   =   xnf   
                              txtbook.Text   &=   xe.GetAttribute("genre")   &   vbCrLf   'Console.WriteLine(xe.GetAttribute("genre"))   
                              txtbook.Text   &=   xe.GetAttribute("ISBN")   &   vbCrLf   'Console.WriteLine(xe.GetAttribute("ISBN"))   
                              Dim   xnfl   As   Xml.XmlNodeList   
                              xnfl   =   xe.ChildNodes   
                              Dim   xn2   As   Xml.XmlNode   
                              For   Each   xn2   In   xnfl   
                                      txtbook.Text   &=   xn2.InnerText   &   vbCrLf   'Console.WriteLine(xn2.InnerText)   
                              Next   
                      Next   
      5、 读取单个数据   
      Dim   xmlDoc   As   New   Xml.XmlDocument   
                      MessageBox.Show(Application.ExecutablePath)   
                      xmlDoc.Load("..\bookstore.xml")   '(Application.ExecutablePath   +   "\..\bookstore.xml")   
                      Dim   tmpNode   As   Xml.XmlNode   
                      Try   
                              tmpNode   =   xmlDoc.Item("bookstore").ChildNodes(0)   
                      Catch   ex   As   Exception   
                              MessageBox.Show(ex.Message)   
                      End   Try   
                      MessageBox.Show(tmpNode.Name)   
                      If   tmpNode.Name   =   "book"   Then   
                              MessageBox.Show(tmpNode.ChildNodes(0).InnerText)   
                              MessageBox.Show(tmpNode.ChildNodes(1).InnerText)   
                              MessageBox.Show(tmpNode.ChildNodes(2).InnerText)   
                      End   If   
    6,绑定到datalist等数据控件 
     Dim stream As FileStream = New FileStream(Server.MapPath("bbs.xml"), FileMode.Open)
                Dim doc As XmlDataDocument = New XmlDataDocument
                doc.DataSet.ReadXml(New StreamReader(stream))
                If doc.DataSet.Tables.Count > 0 And doc.DataSet.Tables(0).Rows.Count > 0 Then
                    mydatalist.DataSource = doc.DataSet.Tables(0).DefaultView
                    mydatalist.DataMember = "title"
                    mydatalist.DataBind()
                End If
                stream.Close()