Data Dictionary dictionary stores key-value pairs and Dictionary dictionary stores
1.Methods:
Use the data Dictionary [Dictionary <string,String>, declares a list set, and stores "XML subnode name" and "Node value" in the form of a key node name value node value pair to this set, then, pass the set as a parameter to the encapsulated public method;
2.Public method:
Public static string AssembleXML (Dictionary <string, string> list) {try {string strXML = ""; foreach (KeyValuePair <string, string> de in list) {strXML + = "<" + de. key + ">" + de. value + "</" + de. key + ">";} return strXML;} catch (Exception ex) {MessageBox. show (ex. message, "error occurred when assembling XML:", MessageBoxButtons. OK, MessageBoxIcon. error); return "0 ";}}
3.Call Public methods:
Static void Main (string [] args) {string strXML exchange = ""; Dictionary <string, string> list = new Dictionary <string, string> (); list. add ("name", "Zhang San"); // list of xml nodes and node values. add ("Age", "20"); string strResult = AssembleXML (list); if (strResult = "0") {MessageBox. show ("xml assembly error! ");} Else {strXML exchange = @" <? Xml version = '1. 0' encoding = 'gbk'?> <ROWSET> <ROW> "+ strResult +" </ROW> </ROWSET> ";} Console. WriteLine (strXML exchange); Console. ReadKey ();}
4. complete code block:
1 using System; 2 using System. collections; 3 using System. collections. generic; 4 using System. linq; 5 using System. text; 6 using System. windows. forms; 7 8 namespace TestPublicXML 9 {10 class Program11 {12 static void Main (string [] args) 13 {14 string strXML interchange = ""; 15 Dictionary <string, string> list = new Dictionary <string, string> (); 16 list. add ("name", "Zhang San"); // xml node, node value 17 list. add ("Age", "20"); 18 str Ing strResult = AssembleXML (list); 19 if (strResult = "0") 20 {21 MessageBox. Show ("xml assembly error! "); 22} 23 else24 {25 strXML exchange = @" <? Xml version = '1. 0' encoding = 'gbk'?> <ROWSET> <ROW> "+ strResult +" </ROW> </ROWSET> "; 26} 27 Console. writeLine (strXML exchange); 28 Console. readKey (); 29} 30 31 public static string AssembleXML (Dictionary <string, string> list) 32 {33 try34 {35 string strXML = ""; 36 foreach (KeyValuePair <string, string> de in list) 37 {38 strXML + = "<" + de. key + ">" + de. value + "</" + de. key + ">"; 39} 40 return strXML; 41} 42 catch (Exception ex) 43 {44 MessageBox. show (ex. message, "error occurred when assembling XML:", MessageBoxButtons. OK, MessageBoxIcon. error); 45 return "0"; 46} 47} 48} 49}View Code