求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、首先在“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文件的实例???
我觉得遍历controls的时候反射出控件类型再直接写进xmldocument中好些
的确简单
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();
至于winform界面上遍历控件,就没看懂楼主的意思了。
我一般都知道界面上有些什么,无需遍历。
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);}