用不到140行C-sharp代码开发面向对象的数据库(下篇)

开发者在线 Builder.com.cn 更新时间:2007-03-02作者:builder.com.cn 来源:

列表B

    <Order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Identity>16d8f0b8-46c6-47c3-ac6b-a0b0e0852970</Identity>
      <CustomerIdentity>61cf2db4-0071-4380-83df-65a102d82ff2</CustomerIdentity>
      <DatePlaced>2006-11-21T07:12:26.0533326-05:00</DatePlaced>
    </Order>

这只是序列化形式的Order对象。

要加载XML数据库,我们就要创建一个XmlDocument对象,并调用它的“Load(加载)”方法来加载XML。这个功能由XmlDBState类的“OpenDatabase”函数来实现。(列表C

列表C

    public static void OpenDatabase(string path)
    {
        //Set the path to the database file.
        _path = path;

        //Douse the database file already exist?
        if (File.Exists(path))
            Database.Load(path); //If so, load it.

        //If a main node already exists in the database
        // use it. If not, create it.
        MainNode = (Database.ChildNodes.Count > 0) ?
            Database.SelectSingleNode(rootName) :
            Database.CreateElement(rootName);

        //If the main node doesn't exist, add it.
        if (Database.SelectSingleNode(rootName) == null)
            Database.AppendChild(MainNode);
    }

这个函数的一个很有意思的特性是,如果数据库文件不存在,它会在内存中自动地创建一个数据库来使用。当对象被保存并被保持到磁盘之后,内存中的数据库就会被写到磁盘上。

上面代码中参考的“MainNode(主节点)”指的是XML的“Database(数据库)”节点。所有的对象都被保存在这个节点下面,它被认为是XML文档的“根节点”。

用户评论

  • 用户名
  • 评论内容