public static IXmlNodeSerializable ReadFromXmlNode(IXmlCodeReader serializer, XmlNode node, params object[] constructorParams) { if (node == null) { throw new XmlSerializationException("Cannot call ReadFromXmlNode with a null node"); } Type t = XmlUtil.GetLibTypeAttribute(node); if (t == null) { throw new XmlSerializationException(string.Format("Xml node {0} having an invalid type attribute: {1}. Check the version of the DLL providing the type.", node.Name, XmlUtil.GetLibTypeAttributeString(node))); } try { if (t.Equals(typeof(ObjectRef))) { constructorParams = new object[] { node }; } IXmlNodeSerializable obj = (IXmlNodeSerializable)Activator.CreateInstance(t, constructorParams); obj.OnReadFromXmlNode(serializer, node); return(obj); } catch (Exception e) { XmlSerializationException er = new XmlSerializationException(string.Format("Xml node {0} with {1} as type attribute cannot create an IXmlNodeSerializable object. See the inner exception for details.", node.Name, XmlUtil.GetLibTypeAttributeString(node)), e); throw er; } }
public static void WriteToXmlNode(IXmlCodeWriter serializer, IXmlNodeSerializable obj, XmlNode node) { if (node == null) { throw new XmlSerializationException("Cannot call WriteToXmlNode with a null node"); } if (obj == null) { throw new XmlSerializationException("Cannot call WriteToXmlNode with a null object. node=" + node.Name); } XmlUtil.SetLibTypeAttribute(node, obj.GetType()); obj.OnWriteToXmlNode(serializer, node); }
public static XmlNode WriteToUniqueChildXmlNode(IXmlCodeWriter serializer, XmlNode nodeParent, string name, IXmlNodeSerializable obj) { if (nodeParent == null) { if (string.IsNullOrEmpty(name)) { throw new XmlSerializationException("Cannot call WriteToUniqueChildXmlNode with a null nodeParent and empty child name."); } else { throw new XmlSerializationException("Cannot call WriteToUniqueChildXmlNode with a null nodeParent. child name=" + name); } } if (string.IsNullOrEmpty(name)) { throw new XmlSerializationException("Cannot call WriteToUniqueChildXmlNode with a null child name. nodeParent=" + nodeParent.Name); } XmlNode node = nodeParent.SelectSingleNode(name); if (node == null) { if (obj != null) { node = nodeParent.OwnerDocument.CreateElement(name); nodeParent.AppendChild(node); } } else { if (obj != null) { node.RemoveAll(); } else { nodeParent.RemoveChild(node); node = null; } } if (obj != null) { WriteToXmlNode(serializer, obj, node); } return(node); }
public static XmlNode WriteToChildXmlNode(IXmlCodeWriter serializer, XmlNode nodeParent, string name, IXmlNodeSerializable obj) { if (nodeParent == null) { if (string.IsNullOrEmpty(name)) { throw new XmlSerializationException("Cannot call WriteToChildXmlNode with a null nodeParent and empty child name."); } else { throw new XmlSerializationException("Cannot call WriteToChildXmlNode with a null nodeParent. child name=" + name); } } if (string.IsNullOrEmpty(name)) { throw new XmlSerializationException("Cannot call WriteToChildXmlNode with a null child name. nodeParent=" + nodeParent.Name); } if (obj == null) { throw new XmlSerializationException("Cannot call WriteToChildXmlNode with a null object. nodeParent=" + nodeParent.Name + ", child name=" + name); } XmlNode node = nodeParent.OwnerDocument.CreateElement(name); nodeParent.AppendChild(node); WriteToXmlNode(serializer, obj, node); return(node); }