扫一扫
分享文章到微信

扫一扫
关注官方公众号
至顶头条
作者:builder.com.cn 2007年3月2日
关键字:
在数据库被加载到XmlDBState对象之后,我们就希望将对象保存在它里面。这也可以通过XmlDBState用一个叫做“SaveObject”的方法来处理。SaveObject的代码见列表D。
列表Dpublic static void SaveObject(IXmlDBSerializable data, bool persistData)
{
    Type type = data.GetType();
    string typeString = type.ToString();
    PropertyInfo[] properties = type.GetProperties();
    //Remove the object if it's currently in the database.
    XmlNode typeNode = RemoveCurrentObject(typeString, data);
    //Loop through each property in our object and see
    // if we need to save them in the database.
    foreach (PropertyInfo property in properties)
    {
        //Get the property's value.
        object propertyValue = property.GetValue(data, null);
        //Check to see if the property is IXmlDBSerializable,
        // and if it is save it to the database.
        if (propertyValue is IXmlDBSerializable)
            ((IXmlDBSerializable)propertyValue).Save(persistData);
        else if (propertyValue is System.Collections.ICollection)
        {
            //This property is a collection of objects.
            // We need to see if this collection contains
            // IXmlDBSerializable objects, and serialize
            // those objects if needed.
            IList propertyList = propertyValue as IList;
            //Does the collection contain IXmlDBSerializable
            // objects?
            if (propertyList != null &&
                propertyList.Count > 0 &&
                propertyList[0] is IXmlDBSerializable)
            {
                //It does contain IXmlDBSerializable objects
                // so save each of them.
                foreach (object listObject in propertyList)
                    ((IXmlDBSerializable)listObject).Save(persistData);
            }
        }
    }
    //If the type which is being saved isn't currently
    // represented in the database, create a place to
    // hold that specific type.
    if (typeNode == null)
    {
        typeNode = XmlDBState.Database.CreateElement(typeString);
        XmlDBState.MainNode.AppendChild(typeNode);
    }
    //Prepare the objects we will need for serializing
    // the object.
    XmlSerializer serializer = new XmlSerializer(type);
    StringWriter writer = new StringWriter();
    XmlDocument objectDocument = new XmlDocument();
    //Serialize the object into our StringWriter object.
    serializer.Serialize(writer, data);
    //Create an XmlDocument from our serialized object.
    objectDocument.InnerXml = writer.ToString();
    //If the serialized object had data, import it into
    // the database.
    if (objectDocument.ChildNodes.Count > 0)
    {
        //Set the object's Node property to the serialized
        // data.
        data.Node = 
            XmlDBState.Database.ImportNode(objectDocument.ChildNodes[1], 
                                            true);
        //Append the serialized object to the type node.
        typeNode.AppendChild(data.Node);
    }
    //If requested, persist these changes to the XML file
    // held on disk. If this is not called, the change is
    // made in memory only.
    if (persistData)
        XmlDBState.Database.Save(XmlDBState.Path);
}
这个函数毫无疑问地担当着数据库最重要的功能。下面就是这个函数从开始到结束所经历的步骤。
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。