/// <summary>
/// XML保存和设置接口
/// </summary>
public interface IXmlStorage
{
/// <summary>
/// 根据对应的XML节点来获取各属性值
/// </summary>
/// <param name="factoTypeNode"> 读取信息的节点 </param>
void GetValueFromXml(System.Xml.XmlNode objectNode);
/// <summary>
/// 将对象本身格式化为XML
/// </summary>
/// <param name="xmldoc"> 需要写入的文档 </param>
/// <param name="parentEle"> 生成节点的父元素节点 </param>
void SetValueToXml(System.Xml.XmlDocument xmldoc, XmlElement parentEle);
}
/// <summary>
/// 数据库对象
/// </summary>
public class DataBase : IXmlStorage
{
/// <summary>
/// 数据库名称
/// </summary>
public string Name = string .Empty;
/// <summary>
/// 描述
/// </summary>
public string Description = string .Empty;
/// <summary>
/// 创建日期
/// </summary>
public DateTime CreateDate = DateTime.Now;
/// <summary>
/// 数据表集合
/// </summary>
public List < Table > TableList = new List < Table > ();
/// <summary>
/// 配置文件实例
/// </summary>
XmlDocument xmldoc = new XmlDocument();
/// <summary>
/// 默认配置文件名称
/// </summary>
public static readonly string DefaultConfigXml = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + Path.DirectorySeparatorChar + " DatabaseConfig.xml " ;
#region IXmlStorage Members
public void GetValueFromXml(XmlNode objectNode)
{
if (objectNode == null )
return ;
if (objectNode.Name.Equals( " database " , StringComparison.CurrentCultureIgnoreCase))
{
XmlAttributeCollection attCol = objectNode.Attributes;
// 从节点属性中获取值
for ( int i = 0 ; i < attCol.Count; i ++ )
{
XmlAttribute att = attCol[i];
if (att.Name.Equals( " name " , StringComparison.CurrentCultureIgnoreCase))
this .Name = att.Value;
if (att.Name.Equals( " description " , StringComparison.CurrentCultureIgnoreCase))
this .Description = att.Value;
if (att.Name.Equals( " date " , StringComparison.CurrentCultureIgnoreCase))
{
try
{
this .CreateDate = DateTime.Parse(att.Value);
}
catch
{
this .CreateDate = DateTime.Now;
}
}
}
// 先清空原来的下级节点内容(因为有可能多次重复加载)
this .TableList.Clear();
// 获取下一级子节点的值
XmlNodeList nodeList = objectNode.ChildNodes;
if (nodeList != null && nodeList.Count > 0 )
{
for ( int j = 0 ; j < nodeList.Count; j ++ )
{
XmlNode node = nodeList[j];
if (node.Name.Equals( " table " , StringComparison.CurrentCultureIgnoreCase))
{
Table t = new Table();
t.GetValueFromXml(node);
this .TableList.Add(t);
}
}
}
}
}
public void SetValueToXml(XmlDocument xmldoc, XmlElement parentEle)
{
XmlElement dbEle = xmldoc.CreateElement( " Database " );
dbEle.SetAttribute( " name " , this .Name);
dbEle.SetAttribute( " description " , this .Description);
dbEle.SetAttribute( " date " , this .CreateDate.ToString());
xmldoc.AppendChild(dbEle); // 由于此时是根节点,故parentEle为空
// 添加下一级的子节点
for ( int i = 0 ; i < this .TableList.Count; i ++ )
{
Table t = this .TableList[i];
t.SetValueToXml(xmldoc, dbEle);
}
}
#endregion
}
/// <summary>
/// 数据表
/// </summary>
public class Table : IXmlStorage
{
/// <summary>
/// 表名
/// </summary>
public string Name = string .Empty;
/// <summary>
/// 描述
/// </summary>
public string Description = string .Empty;
/// <summary>
/// 列集合
/// </summary>
public List < Column > ColumnList = new List < Column > ();
#region IXmlStorage Members
public void GetValueFromXml(System.Xml.XmlNode objectNode)
{
if (objectNode == null )
return ;
if (objectNode.Name.Equals( " table " , StringComparison.CurrentCultureIgnoreCase))
{
XmlAttributeCollection attCol = objectNode.Attributes;
// 从节点属性中获取值
for ( int i = 0 ; i < attCol.Count; i ++ )
{
XmlAttribute att = attCol[i];
if (att.Name.Equals( " name " , StringComparison.CurrentCultureIgnoreCase))
this .Name = att.Value;
if (att.Name.Equals( " description " , StringComparison.CurrentCultureIgnoreCase))
this .Description = att.Value;
}
// 先清空原来的下级节点内容(因为有可能多次重复加载)
this .ColumnList.Clear();
// 获取下一级子节点的值
XmlNodeList nodeList = objectNode.ChildNodes;
if (nodeList != null && nodeList.Count > 0 )
{
for ( int j = 0 ; j < nodeList.Count; j ++ )
{
XmlNode node = nodeList[j];
if (node.Name.Equals( " column " , StringComparison.CurrentCultureIgnoreCase))
{
Column c = new Column();
c.GetValueFromXml(node);
this .ColumnList.Add(c);
}
}
}
}
}
public void SetValueToXml(System.Xml.XmlDocument xmldoc, System.Xml.XmlElement parentEle)
{
XmlElement tblEle = xmldoc.CreateElement( " Table " );
tblEle.SetAttribute( " name " , this .Name);
tblEle.SetAttribute( " description " , this .Description);
parentEle.AppendChild(tblEle); // 由于此时是根节点,故parentEle为空
// 添加下一级的子节点
for ( int i = 0 ; i < this .ColumnList.Count; i ++ )
{
Column c = this .ColumnList[i];
c.SetValueToXml(xmldoc, tblEle);
}
}
#endregion
}
#region 读取和保存XML文件
public bool LoadFromFile( string xmlPath)
{
try
{
this .xmldoc.Load(xmlPath);
XmlNode rootNode = this .xmldoc.SelectSingleNode( " //Database " );
if (rootNode != null )
{
this .GetValueFromXml(rootNode);
}
}
catch (Exception ex)
{
string msg = " 加载配置文件失败,发生异常: " + ex.Message;
throw new Exception(msg);
}
return true ;
}
public bool SaveToFile( string xmlPath)
{
try
{
this .xmldoc = new XmlDocument(); // 重新构造一个XML文档
XmlDeclaration xDeclare = this .xmldoc.CreateXmlDeclaration( " 1.0 " , " GB2312 " , null );
this .xmldoc.AppendChild(xDeclare);
this .SetValueToXml( this .xmldoc, null );
this .xmldoc.Save(xmlPath);
return true ;
}
catch (XmlException ex)
{
throw new Exception( " 保存配置文件失败,发生异常: " + ex.Message);
}
}
#endregion
DataBase db = new DataBase();
db.Name = " 数据库1 " ;
db.Description = " for test " ;
Table tbl = new Table();
tbl.Name = " Student " ;
tbl.Description = " 学生信息表 " ;
db.TableList.Add(tbl);
Column c1 = new Column();
c1.Name = " StudentName " ;
c1.ColumnType = 1 ;
c1.IsNullable = false ;
c1.Remark = " 姓名 " ;
Column c2 = new Column();
c2.Name = " Number " ;
c2.ColumnType = 1 ;
c2.IsPrimaryKey = true ;
c2.Remark = " 学号 " ;
Column c3 = new Column();
c3.Name = " Age " ;
c3.ColumnType = 2 ;
c3.Remark = " 年龄 " ;
tbl.ColumnList.Add(c1);
tbl.ColumnList.Add(c2);
tbl.ColumnList.Add(c3);
// 保存数据到XML文件
db.SaveToFile( " D:\\mytest.xml " );
// 从XML文件中加载数据
DataBase db2 = new DataBase();
db2.LoadFromFile( " D:\\mytest.xml " );