求C#操作XML文件的实例和思想。功能描述:在“Winform窗体”中有“Lable”、“TextBox”、“Button”、“CommoBox”几个控件。现在要遍历“窗体”中的控件,创建xml文件。
1、首先在“xml”文件中要有“<winform name="" width="" height="" color="" title=""></winform>”标记,在其中有<lable></lable>、<textbox></textbox>、<button></button>、<commobox></commobox>等控件的标记。2、如果为“Button”,那么就在xml文件中添加标记“<button  Name=""  Width=""  Height="" Color="" SQL="" Text="" Event=""></button>”,Name:控件名称,Width:控件宽度,Height:控件高度,Color:颜色,Text:文本内容,SQL:要执行的SQL语句。Event:要执行的事件。3、如果为“Lable”,那么就在xml文件中添加标记“<lable name="" width="" height="" color="" text="" FontSize=""  FontColor="" >”,name:控件名称,Name:控件名称,Width:控件宽度,Height:控件高度,Color:颜色,Text:文本内容,FontSize:字体大小,FontColor:字体颜色。4、如果为“CommoBox”,那么就在xml文件中添加标记“<commobox name="" width="" height="" color="" text=""  value="" dropdownlist="" >”,name:控件名称,width:控件宽度,height:控件高度,color:颜色,text:被选择文本内容,value:被选择值,dropdownlist:选择项(键值对,键是text文本,value是值)。5、如果为“textbox”,那么就在xml文件中添加标记<textbox  name="" width="" height="" bgcolor="" text="" FontSize=""  FontColor=""></textbox>,Name:控件名称,Width:控件宽度,Height:控件高度,Color:颜色,Text:文本内容,FontSize:字体大小,FontColor:字体颜色。6、标签之间可以嵌套,最多可以嵌套两层。比如:<textbox><button></button></textbox>,表示“Button”按钮紧紧的跟在“Textbox”的后面。不能在“<button></button>”里面添加其它元素。有没有类似的创建、读写XML文件的实例???

解决方案 »

  1.   

    懒得讲,遍历controls,自己判断是不是你要的类型,xmldocument写xml,自己看帮助
      

  2.   

    C#创建XML文档的一种方法的源程序代码using System ;using System.Xml ;class MainClass{  XmlDocument xmldoc ;  XmlNode xmlnode ;  XmlElement xmlelem ;  XmlElement xmlelem2 ;  XmlText xmltext ;  static void Main ( string [ ] args )  {  MainClass app = new MainClass ( ) ;  }  public MainClass ( )  {    xmldoc = new XmlDocument ( ) ;    //加入XML的声明段落    xmlnode = xmldoc.CreateNode ( XmlNodeType.XmlDeclaration , "" , "" ) ;    xmldoc.AppendChild ( xmlnode ) ;    //加入一个根元素    xmlelem = xmldoc.CreateElement ( "" , "ROOT" , "" ) ;    xmltext = xmldoc.CreateTextNode ( "Root Text" ) ;    xmlelem.AppendChild ( xmltext ) ;    xmldoc.AppendChild ( xmlelem ) ;    //加入另外一个元素    xmlelem2 = xmldoc.CreateElement ("SampleElement" ) ;    xmlelem2 = xmldoc.CreateElement ( "" , "SampleElement" , "" ) ;    xmltext = xmldoc.CreateTextNode ( "The text of the sample element" ) ;    xmlelem2.AppendChild ( xmltext ) ;    xmldoc.ChildNodes.Item(1).AppendChild ( xmlelem2 ) ;    //保存创建好的XML文档    try    {      xmldoc.Save ( "c:\\data.xml" ) ;    }    catch ( Exception e )    {      //显示错误信息      Console.WriteLine ( e.Message ) ;    }    Console.ReadLine ( ) ;  }} 
      

  3.   

    C#读取XML的文件    具体如下:using System ;using System.Drawing ;using System.Collections ;using System.ComponentModel ;using System.Windows.Forms ;using System.Data ;using System.Xml ;public class Form1 : Form{ private Button button1 ; private ListView Listview1 ; private System.ComponentModel.Container components = null ; public Form1 ( ) { //初始化窗体中的各个组件 InitializeComponent ( ) ; } //清除程序中使用过的资源 protected override void Dispose ( bool disposing ) { if ( disposing ) { if ( components != null ) { components.Dispose ( ) ; } } base.Dispose ( disposing ) ; } private void InitializeComponent ( ) { button1 = new Button ( ) ; Listview1 = new ListView ( ) ; SuspendLayout ( ) ; button1.Anchor = ( ( AnchorStyles.Bottom | AnchorStyles.Left ) | AnchorStyles.Right ) ; button1.Location = new Point ( 240 , 296 ) ; button1.Name = "button1" ; button1.Size = new Size ( 112 , 37 ) ; button1.TabIndex = 0 ; button1.Text = "读取XML文档" ; button1.Click += new System.EventHandler ( button1_Click ) ; Listview1.Anchor = ( ( ( AnchorStyles.Top | AnchorStyles.Bottom ) | AnchorStyles.Left ) | AnchorStyles.Right ) ; Listview1.GridLines = true ; Listview1.Location = new Point ( 10 , 9 ) ; Listview1.Name = "Listview1" ; Listview1.Size = new Size ( 623 , 269 ) ; Listview1.TabIndex = 1 ; Listview1.View = View.Details ; this.AutoScaleBaseSize = new Size ( 6 , 14 ) ; this.ClientSize = new Size ( 608 , 348 ) ; this.Controls.Add ( Listview1 ); this.Controls.Add ( button1 ); this.Name = "Form1" ; this.StartPosition = FormStartPosition.CenterScreen ; this.Text = "用C#来读取XML文档" ; this.ResumeLayout ( false ) ; } static void Main ( ) { Application.Run ( new Form1 ( ) ) ; } private void button1_Click ( object sender , System.EventArgs e ) { ListViewItem myItem = new ListViewItem ( ) ; // 构建listview组件 Listview1.Columns.Clear ( ) ; Listview1.Items.Clear ( ) ; Listview1.Columns.Add ( "Name" , 80 , HorizontalAlignment.Left ) ; Listview1.Columns.Add ( "Zip" , 80 , HorizontalAlignment.Left ) ; Listview1.Columns.Add ( "Address" , 80 , HorizontalAlignment.Left ) ; Listview1.Columns.Add ( "City" , 80 , HorizontalAlignment.Left ) ; Listview1.Columns.Add ( "State" , 80 , HorizontalAlignment.Left ) ; XmlNodeReader reader = null ; try { string s = "" ; XmlDocument doc = new XmlDocument ( ) ; // 装入指定的XML文档 doc.Load ( "C:\\data.xml" ) ; // 设定XmlNodeReader对象来打开XML文件 reader = new XmlNodeReader ( doc ) ; // 读取XML文件中的数据,并显示出来 while ( reader.Read ( ) ) { //判断当前读取得节点类型 switch ( reader.NodeType ) { case XmlNodeType.Element : s = reader.Name ; break ; case XmlNodeType.Text : if ( s.Equals ( "Name" ) ) myItem = Listview1.Items.Add ( reader.Value ) ; else myItem.SubItems.Add ( reader.Value ) ; break ; } } } finally { //清除打开的数据流 if ( reader != null ) reader.Close ( ) ; } }} 
      

  4.   

    这种方法有点笨,
    我觉得遍历controls的时候反射出控件类型再直接写进xmldocument中好些
      

  5.   

    对应XML的操作,网上有很多现成的例子,MSDN也有详细介绍,这个需要你自己一点一点体会: 使用XmlDocument创建XML文档及增加删除更新节点 C#使用XmlDocument操作XML进行查询、增加、修改、删除、保存应用的实例  
      

  6.   

    你要仔细想想你要干什么,如果是储存用户自定义的页面控件,建议采用序列化的方式来储存,如果用xml来存储只能自己一点一点写代码,写一个类来完成你的功能。
      

  7.   

    Linq To XML
    的确简单
      

  8.   

    事件里贴如下源码就可以了,直接导出xml。可以看下效果
           
            System.IO.StringWriter sw = new StringWriter();        sw.WriteLine("<?xml version=\"1.0\" encoding=\"gb2312\"?>");        sw.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>");        sw.WriteLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"");        sw.WriteLine(" xmlns:o=\"urn:schemas-microsoft-com:office:office\"");        sw.WriteLine(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");        sw.WriteLine(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"");        sw.WriteLine(" xmlns:html=\"http://www.w3.org/TR/REC-html40\">");        sw.WriteLine(" <DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">");        sw.WriteLine("  <Created>1996-12-17T01:32:42Z</Created>");        sw.WriteLine("  <LastSaved>2009-07-22T05:39:13Z</LastSaved>");        sw.WriteLine("  <Version>11.6408</Version>");        sw.WriteLine(" </DocumentProperties>");        sw.WriteLine(" <OfficeDocumentSettings xmlns=\"urn:schemas-microsoft-com:office:office\">");        sw.WriteLine("  <RemovePersonalInformation/>");        sw.WriteLine(" </OfficeDocumentSettings>");        sw.WriteLine(" <ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">");        sw.WriteLine("  <WindowHeight>4530</WindowHeight>");        sw.WriteLine("  <WindowWidth>8505</WindowWidth>");        sw.WriteLine("  <WindowTopX>480</WindowTopX>");        sw.WriteLine("  <WindowTopY>120</WindowTopY>");        sw.WriteLine("  <AcceptLabelsInFormulas/>");        sw.WriteLine("  <ProtectStructure>False</ProtectStructure>");        sw.WriteLine("  <ProtectWindows>False</ProtectWindows>");        sw.WriteLine(" </ExcelWorkbook>");        sw.WriteLine(" <Styles>");        sw.WriteLine("  <Style ss:ID=\"Default\" ss:Name=\"Normal\">");        sw.WriteLine("   <Alignment ss:Vertical=\"Bottom\"/>");        sw.WriteLine("   <Borders/>");        sw.WriteLine("   <Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Size=\"12\"/>");        sw.WriteLine("   <Interior/>");        sw.WriteLine("   <NumberFormat/>");        sw.WriteLine("   <Protection/>");        sw.WriteLine("  </Style>");        sw.WriteLine("  <Style ss:ID=\"s21\">");        sw.WriteLine("   <Borders/>");        sw.WriteLine("  </Style>");        sw.WriteLine("  <Style ss:ID=\"s22\">");        sw.WriteLine("   <Borders/>");        sw.WriteLine("   <NumberFormat ss:Format=\"@\"/>");        sw.WriteLine("  </Style>");        sw.WriteLine(" </Styles>");        sw.WriteLine(" <Worksheet ss:Name=\"Sheet1\">");        sw.WriteLine("  <Table ss:ExpandedColumnCount=\"5\" ss:ExpandedRowCount=\"50\" x:FullColumns=\"1\"");        sw.WriteLine("   x:FullRows=\"1\" ss:StyleID=\"s21\" ss:DefaultColumnWidth=\"54\"");        sw.WriteLine("   ss:DefaultRowHeight=\"14.25\">");        sw.WriteLine("   <Column ss:StyleID=\"s21\" ss:AutoFitWidth=\"0\" ss:Width=\"72\"/>");        sw.WriteLine("   <Column ss:StyleID=\"s21\" ss:AutoFitWidth=\"0\" ss:Width=\"87.75\"/>");        sw.WriteLine("   <Column ss:StyleID=\"s22\" ss:Width=\"57\"/>");        sw.WriteLine("   <Column ss:StyleID=\"s21\" ss:AutoFitWidth=\"0\" ss:Width=\"93.75\"/>");        sw.WriteLine("   <Column ss:StyleID=\"s22\" ss:Width=\"57\"/>");        sw.WriteLine("   <Row>");        sw.WriteLine("    <Cell><Data ss:Type=\"String\">排产周</Data></Cell>");        sw.WriteLine("    <Cell><Data ss:Type=\"String\">工单号</Data></Cell>");        sw.WriteLine("    <Cell><Data ss:Type=\"String\">工单行号</Data></Cell>");        sw.WriteLine("    <Cell><Data ss:Type=\"String\">客户号</Data></Cell>");        sw.WriteLine("    <Cell><Data ss:Type=\"String\">客户行号</Data></Cell>");        sw.WriteLine("   </Row>");        sw.WriteLine("   <Row>");        sw.WriteLine("    <Cell ss:Index=\"3\" ss:StyleID=\"s21\"/>");        sw.WriteLine("   </Row>");        sw.WriteLine("  </Table>");        sw.WriteLine("  <WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">");        sw.WriteLine("   <Selected/>");        sw.WriteLine("   <Panes>");        sw.WriteLine("    <Pane>");        sw.WriteLine("     <Number>3</Number>");        sw.WriteLine("     <ActiveRow>6</ActiveRow>");        sw.WriteLine("     <ActiveCol>1</ActiveCol>");        sw.WriteLine("    </Pane>");        sw.WriteLine("   </Panes>");        sw.WriteLine("   <ProtectObjects>False</ProtectObjects>");        sw.WriteLine("   <ProtectScenarios>False</ProtectScenarios>");        sw.WriteLine("  </WorksheetOptions>");        sw.WriteLine("  <Sorting xmlns=\"urn:schemas-microsoft-com:office:excel\">");        sw.WriteLine("   <Sort>工单号</Sort>");        sw.WriteLine("  </Sorting>");        sw.WriteLine(" </Worksheet>");        sw.WriteLine("</Workbook>");         Response.Clear();        Response.Charset = "gb2312";        Response.AddHeader("content-disposition", "attachment;filename=" + Server.UrlEncode("Template.xls"));         //Response.ContentEncoding = System.Text.Encoding.Default;        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");        Response.ContentType = "application/ms-excel";  // ms-text        HttpContext.Current.Response.Write(sw.ToString());        Response.End();
      

  9.   

    XML读写
      

  10.   

    生成XML文件就跟生成文本文件是一个原理啊~
    至于winform界面上遍历控件,就没看懂楼主的意思了。
    我一般都知道界面上有些什么,无需遍历。
      

  11.   

    对XML的操作可以通过DataSet来实现,有这样的实例下载吗???
      

  12.   


    DataSet.ReadXml()....private void DemonstrateReadWriteXMLDocumentWithStreamReader()
    {
        // Create a DataSet with one table and two columns.
        DataSet OriginalDataSet = new DataSet("dataSet");
        OriginalDataSet.Namespace= "NetFrameWork";
        DataTable table = new DataTable("table");
        DataColumn idColumn = new DataColumn("id", 
            Type.GetType("System.Int32"));
        idColumn.AutoIncrement= true;    DataColumn itemColumn = new DataColumn("item");
        table.Columns.Add(idColumn);
        table.Columns.Add(itemColumn);
        OriginalDataSet.Tables.Add(table);    // Add ten rows.
        DataRow newRow;
        for(int i = 0; i < 10; i++)
        {
            newRow = table.NewRow();
            newRow["item"]= "item " + i;
            table.Rows.Add(newRow);
        }
        OriginalDataSet.AcceptChanges();    // Print out values of each table in the DataSet 
        // using the function defined below.
        PrintValues(OriginalDataSet, "Original DataSet");    // Write the schema and data to an XML file.
        string xmlFilename = "XmlDocument.xml";    // Use WriteXml to write the document.
        OriginalDataSet.WriteXml(xmlFilename);    // Dispose of the original DataSet.
        OriginalDataSet.Dispose();    // Create a new DataSet.
        DataSet newDataSet = new DataSet("New DataSet");    // Read the XML document into the DataSet.
        newDataSet.ReadXml(xmlFilename);}