Пример #1
0
        public virtual IXmlable FromXml(XmlElement xmlEle)
        {
            if (Object.ReferenceEquals(null, xmlEle))
            {
                return(null);
            }

            ConstructorInfo constructInfo = null;
            bool            isUnique      = false;

            if (!_xmlFormater.GetXmlTypeInfo(xmlEle.Name, out constructInfo, out isUnique))
            {
                System.Diagnostics.Debug.Assert(false, "could not find the Type of tag name " + xmlEle.Name);
                return(null);
            }
            if (!Object.ReferenceEquals(null, constructInfo))
            {
                IXmlable objXmlable = null;
                if (isUnique)
                {
                    String strId = xmlEle.GetAttribute(_xmlIdAttr);
                    if (!String.IsNullOrEmpty(strId))
                    {
                        if (!_xmlIdToObj.TryGetValue(strId, out objXmlable))
                        {
                            objXmlable = constructInfo.Invoke(_emptyArgument) as IXmlable;
                            _objToXmlId.Add(objXmlable, strId);
                            _xmlIdToObj.Add(strId, objXmlable);
                        }
                        System.Diagnostics.Debug.Assert(null != objXmlable);

                        objXmlable.FromXml(xmlEle, this);
                        return(objXmlable);
                    }
                }

                if (null == objXmlable)
                {
                    objXmlable = constructInfo.Invoke(_emptyArgument) as IXmlable;
                }
                objXmlable.FromXml(xmlEle, this);
                return(objXmlable);
            }
            System.Diagnostics.Debug.Assert(false, "could not load " + xmlEle.Name);
            return(null);
        }
Пример #2
0
        public void FromXml(string strXml)
        {
            if (String.IsNullOrEmpty(this.XmlRoot))
            {
                Debug.Assert(false, "FromXml(strXml) IsNullOrEmpty");
                throw new ArgumentNullException();
            }
            this.Clear();
            XElement element = XElement.Parse(strXml);

            foreach (XElement e in element.Elements())
            {
                IXmlable obj = (IXmlable)Activator.CreateInstance <T>();
                obj.FromXml(e.ToString());
                this.Add((T)obj);
            }
        }
Пример #3
0
        /// <summary>
        /// 克隆对象,并返回一个已克隆对象的引用
        /// 该方法使用 ToXml和 FromXml 方法来实现
        /// </summary>
        public virtual object Clone()
        {
            object newObject = Activator.CreateInstance(this.GetType());

            Type xmlableThis      = this.GetType().GetInterface("IXmlable", true);
            Type xmlableNewObject = this.GetType().GetInterface("IXmlable", true);

            if (xmlableThis == null || xmlableNewObject == null)
            {
                Debug.Assert(false, "调用对象的 Clone()方法,但是没有实现 IXmlable");
                return(null);
            }

            IXmlable iXmlableThis      = (IXmlable)this;
            IXmlable iXmlableNewObject = (IXmlable)newObject;

            iXmlableNewObject.FromXml(iXmlableThis.ToXml());

            return(newObject);
        }
Пример #4
0
        public virtual XmlElement ToXml(IXmlable xmlable)
        {
            if (Object.ReferenceEquals(null, xmlable))
            {
                return(null);
            }

            String xmlName  = null;
            bool   isUnique = false;

            if (!_xmlFormater.GetTypeXmlInfo(xmlable.GetType(), out xmlName, out isUnique))
            {
                System.Diagnostics.Debug.Assert(false, "could not find the Xmlable information of " + xmlable.GetType().ToString());
                return(null);
            }
            if (!String.IsNullOrEmpty(xmlName))
            {
                if (isUnique)
                {
                    String strObjId = null;
                    if (_objToXmlId.TryGetValue(xmlable, out strObjId) && !String.IsNullOrEmpty(strObjId))
                    {
                        XmlElement ele = this.Document.CreateElement(xmlName);
                        XmlToolkit.SetAttribute(ele, _xmlIdAttr, strObjId);
                        return(ele);
                    }
                }

                XmlElement ele2 = this.Document.CreateElement(xmlName);
                if (isUnique)
                {
                    String xmlId = Guid.NewGuid().ToString();
                    _objToXmlId.Add(xmlable, xmlId);
                    _xmlIdToObj.Add(xmlId, xmlable);
                    XmlToolkit.SetAttribute(ele2, _xmlIdAttr, xmlId);
                }
                xmlable.ToXml(ele2, this);
                return(ele2);
            }
            return(null);
        }
Пример #5
0
        public virtual void Serialize(XmlDocument doc, Object graph)
        {
            if (Object.ReferenceEquals(null, doc))
            {
                throw new ArgumentNullException("doc");
            }
            if (Object.ReferenceEquals(null, graph))
            {
                throw new ArgumentNullException("graph");
            }
            IXmlable objXmlable = graph as IXmlable;

            if (Object.ReferenceEquals(null, objXmlable))
            {
                throw new ArgumentException("can't convert to IXmlable", "graph");
            }

            doc.RemoveAll();

            XmlContext cnt = new XmlContext(this, doc);
            XmlElement ele = cnt.ToXml(objXmlable);

            doc.AppendChild(ele);
        }