1. Obtain the XML value based on the key value.
<? XML version = "1.0" standalone = "no"?>
<! -- This is a comment -->
<Configsections>
<Deleetask>
<Add key = "mysqlcon" value = "database = test; Data Source = 127.0.0.1; user id = root; Password = root; pooling = false; charset = utf8; port = 3306 "type =" codeph "text ="/codeph "/>
<Add key = "csvfilepath" value = "E: \ demo \ bin \ debug \"/>
</Appsettings>
</Configsections>
/// <Summary>
/// Obtain the value based on the key value
/// </Summary>
/// <Param name = "configkey"> key value </param>
/// <Returns> value </returns>
Public static string readconfig (string configkey)
{
String configvalue = "";
Xmldocument Doc = new xmldocument ();
// Import the XML file
Doc. Load (_ filepath );
// Find all elements named "add"
Xmlnodelist nodes = Doc. getelementsbytagname ("add ");
For (INT I = 0; I <nodes. Count; I ++)
{
// Obtain the key attribute of the current element
Xmlattribute ATT = nodes [I]. attributes ["key"];
// Determine whether the current element is a target element based on the first attribute of the element.
If (Att. value = "" + configkey + "")
{
// Assign values to the second attribute of the target Element
ATT = nodes [I]. attributes ["value"];
Configvalue = Att. value;
Break;
}
}
Return configvalue;
}
2. generate an XML file
Private void btnclose_click (Object sender, eventargs E)
{
Xmltextwriter myxmltextwriter = new xmltextwriter ("swms_setting.xml", null );
Myxmltextwriter. Formatting = formatting. indented;
Myxmltextwriter. writestartdocument (false );
Myxmltextwriter. writecomment ("this is a comment ");
String sqlconstr = configfactory. readconfig ("mysqlcon ");
Mysqlconnection mycon = new mysqlconnection (sqlconstr );
Mycon. open ();
Try
{
String sqlstr = "select * from work ";
Mysqlcommand command = new mysqlcommand (sqlstr, mycon );
Datatable dt = new datatable ();
Mysqldataadapter SDA = new mysqldataadapter (command );
SDA. Fill (DT );
Myxmltextwriter. writestartelement ("configsections ");
Myxmltextwriter. writestartelement ("works", null );
For (INT I = 0; I <dgv. Rows. Count; I ++)
{
String workid = DT. Rows [I]. itemarray [0]. tostring ();
String workname = DT. Rows [I]. itemarray [1]. tostring ();
String Itemid = DT. Rows [I]. itemarray [2]. tostring ();
String itemname = "";
Stringbuilder selectstr = new stringbuilder ();
Selectstr. append ("select item from item where Itemid in ");
Selectstr. append ("(" + Itemid. tostring () + ")");
Mysqlcommand cmd = new mysqlcommand (selectstr. tostring (), mycon );
Datatable dt_item = new datatable ();
Mysqldataadapter sda_item = new mysqldataadapter (CMD );
Sda_item.fill (dt_item );
For (Int J = 0; j <dt_item.rows.count; j ++)
{
Itemname = itemname + ',' + dt_item.rows [J]. itemarray [0]. tostring ();
}
Itemname = itemname. substring (1, itemname. Length-1 );
Myxmltextwriter. writestartelement ("works", null );
Myxmltextwriter. writeelementstring ("workid", null, workid );
Myxmltextwriter. writeelementstring ("workname", null, workname );
Myxmltextwriter. writeelementstring ("Itemid", null, Itemid );
Myxmltextwriter. writeelementstring ("itemname", null, itemname );
Myxmltextwriter. writeendelement ();
}
Myxmltextwriter. writeendelement ();
Myxmltextwriter. Flush ();
Myxmltextwriter. Close ();
Console. Readline ();
MessageBox. Show ("the XML file is generated successfully! "," Succeeded ", messageboxbuttons. OK, messageboxicon. information );
}
Catch
{
MessageBox. Show ("Err ");
}
// Copyfiletopda (@ "swms_setting.xml", @ "\ temp \ swms_setting.xml ");
This. Close ();
}
The generated XML file is as follows:
<? XML version = "1.0" standalone = "no"?>
<! -- This is a comment -->
<Configsections>
<Works>
<Works>
<Workid> 001 </workid>
<Workname> inbound Load Scheduling </workname>
<Itemid> 0001,0002, 0003,0004, 0005,0006, 0007,0008 </Itemid>
<Itemname> when a product is created, the predefined quantity, the product name, the number of items, and the number of items are reserved. When the business starts, venue </itemname>
</Works>
<Works>
<Workid> 002 </workid>
<Workname> incoming product </workname>
<Itemid> 0009, </Itemid>
<Itemname> location. When the business starts, the business is completed on a daily basis. Result </itemname>
</Works>
<Works>
<Workid> 003 </workid>
<Workname> incoming worker </workname>
<Itemid> 0001,0002 </Itemid>
<Itemname> Commodity Quota quota, set quantity </itemname>
</Works>
<Works>
<Workid> 004 </workid>
<Workname> outbound billing </workname>
<Itemid> 0001,0002 </Itemid>
<Itemname> Commodity Quota quota, set quantity </itemname>
</Works>
<Works>
<Workid> 005 </workid>
<Workname> pick </workname>
<Itemid> 0001,0002 </Itemid>
<Itemname> Commodity Quota quota, set quantity </itemname>
</Works>
<Works>
<Workid> 006 </workid>
<Workname> out-of-charge combination </workname>
<Itemid> 0001,0002 </Itemid>
<Itemname> Commodity Quota quota, set quantity </itemname>
</Works>
<Works>
<Workid> 007 </workid>
<Workname> delivery </workname>
<Itemid> 0001,0002 </Itemid>
<Itemname> Commodity Quota quota, set quantity </itemname>
</Works>
<Works>
<Workid> 008 </workid>
<Workname> shed unload </workname>
<Itemid> 0001,0002 </Itemid>
<Itemname> Commodity Quota quota, set quantity </itemname>
</Works>
<Works>
<Workid> 009 </workid>
<Workname> mobile </workname>
<Itemid> 0001,0002 </Itemid>
<Itemname> Commodity Quota quota, set quantity </itemname>
</Works>
<Works>
<Workid> 010 </workid>
<Workname> inbound </workname>
<Itemid> 0001,0002 </Itemid>
<Itemname> Commodity Quota quota, set quantity </itemname>
</Works>
<Works>
<Workid> 011 </workid>
<Workname> departure </workname>
<Itemid> 0001,0002 </Itemid>
<Itemname> Commodity Quota quota, set quantity </itemname>
</Works>
<Works>
<Workid> 012 </workid>
<Workname> worker </workname>
<Itemid> 0001,0002 </Itemid>
<Itemname> Commodity Quota quota, set quantity </itemname>
</Works>
<Works>
<Workid> 013 </workid>
<Workname> worker </workname>
<Itemid> 0001,0002 </Itemid>
<Itemname> Commodity Quota quota, set quantity </itemname>
</Works>
<Works>
<Workid> 014 </workid>
<Workname> worker </workname>
<Itemid> 0001,0002 </Itemid>
<Itemname> Commodity Quota quota, set quantity </itemname>
</Works>
<Works>
<Workid> 015 </workid>
<Workname> processing </workname>
<Itemid> 0001,0002 </Itemid>
<Itemname> Commodity Quota quota, set quantity </itemname>
</Works>
</Works>
</Configsections>