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); }
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); } }
/// <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); }