using System; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml; using System.Xml.Serialization; using System.IO; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private string m_strXML = ""; // 将二维数组序列化成XML private void button1_Click(object sender, EventArgs e) { string[][] str = { new string[] { "1","2","3" }, new string[] { "A","B","C" } }; XmlSerializer xml = new XmlSerializer( str.GetType() ); System.IO.MemoryStream ms = new System.IO.MemoryStream(); XmlTextWriter writer = new XmlTextWriter(ms, Encoding.Default); xml.Serialize(writer, str); // 得到序列化后的XML字符串,可以直接保存到数据库 m_strXML = Encoding.Default.GetString(ms.ToArray()); MessageBox.Show(m_strXML); } // 把XML反序列化为二维数组 private void button2_Click(object sender, EventArgs e) { // 从数据库取出XML字符串,这里使用m_strXML变量 XmlSerializer xml = new XmlSerializer( typeof(string[][]) ); StreamReader sr = new StreamReader(new MemoryStream(System.Text.Encoding.Default.GetBytes(m_strXML)), System.Text.Encoding.Default); string[][] str=(string[][])xml.Deserialize(sr); foreach (string[] s1 in str) { foreach (string s2 in s1) { MessageBox.Show(s2); } } } }