获取不同数据库表信息将筛选出来的信息序列化以及反序列化
相应类结构:
Class Tables:
[Serializable]
[XmlRoot("Table")]
public class Tables
{
public Tables()
{
Items = new List<ColumnsItems>();
} /// <summary>
/// 新表名称
/// </summary>
[XmlElementAttribute("NewTableName")]
public string NewTableName { get; set; } /// <summary>
/// 旧表名称
/// </summary>
[XmlElementAttribute("OldTableName")]
public string OldTableName { get; set; } [XmlElementAttribute("Field")]
public List<ColumnsItems> Items { get; set; } }
Class ColumnsItems:
[Serializable]
[XmlRoot("Field")]
public class ColumnsItems
{
/// <summary>
/// 新表字段
/// </summary>
[XmlElementAttribute("NewColumn")]
public string NewColumn { get; set; } /// <summary>
/// 旧表字段
/// </summary>
[XmlElementAttribute("OldColumn")]
public string OldColumn { get; set; }
}
Class createXml:
[Serializable]
[XmlRoot("Tables")]
public class createXml
{
public createXml()
{
setTable = new List<Tables>();
}
[XmlElementAttribute("Table")]
public List<Tables> setTable { get; set; }
}
对比表格序列化:
#region 将对比表格序列化
/// <summary>
/// 保存信息事件,将表中数据对应关系读取到XML文件中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void saveInfo_Click(object sender, EventArgs e)
{
XmlDocument xd = new XmlDocument(); // 序列化赋值对象
List<Tables> allTable = new List<Tables>();
//Tables getTable = new Tables();
//ColumnsItems columnItem = new ColumnsItems(); // 读取字段信息
int i = dataGridView2.RowCount-;
for (int j = ; j < i;j++)
{
////
Tables getTable = new Tables();
ColumnsItems columnItem = new ColumnsItems();
///
if (dataGridView2.Rows[j].Cells[].Value.ToString() != null)
{
getTable.NewTableName = dataGridView2.Rows[j].Cells[].Value.ToString();
}
if (dataGridView2.Rows[j].Cells[].Value != null)
{
columnItem.NewColumn = dataGridView2.Rows[j].Cells[].Value.ToString();
}
if (dataGridView2.Rows[j].Cells[].Value != null)
{
getTable.OldTableName = dataGridView2.Rows[j].Cells[].Value.ToString();
}
if (dataGridView2.Rows[j].Cells[].Value != null)
{
columnItem.OldColumn = dataGridView2.Rows[j].Cells[].Value.ToString();
}
if (columnItem != null)
{
getTable.Items.Add(columnItem);
while (dataGridView2.Rows[j].Cells[].Value == dataGridView2.Rows[j + ].Cells[].Value && dataGridView2.Rows[j].Cells[].Value == dataGridView2.Rows[j+].Cells[].Value)
{
ColumnsItems itemTemp = new ColumnsItems();
if (dataGridView2.Rows[j+].Cells[].Value != null)
{
itemTemp.NewColumn = dataGridView2.Rows[j + ].Cells[].Value.ToString();
}
if (dataGridView2.Rows[j+].Cells[].Value != null)
{
itemTemp.OldColumn = dataGridView2.Rows[j + ].Cells[].Value.ToString();
}
getTable.Items.Add(itemTemp);
j++;
if (j >= i)
{
break;
}
}
}
allTable.Add(getTable);
}
createXml setXml = new createXml();
setXml.setTable = allTable;
using(StringWriter wr = new StringWriter())
{
XmlSerializer createXml = new XmlSerializer(typeof(createXml));
createXml.Serialize(wr,setXml);
xd.LoadXml(wr.ToString());
xd.Save("c:\\1.xml");
}
}
#endregion
反序列化函数:
#region 反序列化函数
/// <summary>
/// 反序列化函数
/// </summary>
/// <param name="xmlPath"></param>
/// <returns></returns>
public createXml Deserialize(string xmlPath)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlPath);
try
{
using (StringReader sr = new StringReader(xmlDoc.OuterXml))
{
XmlSerializer xmldes = new XmlSerializer(typeof(createXml));
createXml getObjXml = (createXml)xmldes.Deserialize(sr);
return getObjXml;
}
}
catch (Exception)
{
throw;
}
}
#endregion
效果图片: