public static ChangeTypeForXML ( object value, Type type ) : object | ||
value | object | |
type | Type | |
Résultat | object |
public override string ConvertObjectToXml(object value) { if ((value == null) || (value == _nullValue))// this case won't happen, this is added in case if code in xml saver changes { return(string.Empty); } Type type = _dataType; if (type == typeof(byte[]) || (type == typeof(object) && (value is byte[]))) { return(Convert.ToBase64String((byte[])value)); } if ((type == typeof(Type)) || ((type == typeof(object)) && (value is Type))) { return(((Type)value).AssemblyQualifiedName !); } if (!IsTypeCustomType(value.GetType())) { // Guid and Type had TypeCode.Object return((string)SqlConvert.ChangeTypeForXML(value, typeof(string))); } if (Type.GetTypeCode(value.GetType()) != TypeCode.Object) { return(value.ToString() !); } StringWriter strwriter = new StringWriter(FormatProvider); if (_implementsIXmlSerializable) { using (XmlTextWriter xmlTextWriter = new XmlTextWriter(strwriter)) { ((IXmlSerializable)value).WriteXml(xmlTextWriter); } return(strwriter.ToString()); } XmlSerializer serializerWithOutRootAttribute = ObjectStorage.GetXmlSerializer(value.GetType()); serializerWithOutRootAttribute.Serialize(strwriter, value); return(strwriter.ToString()); }
public override string ConvertObjectToXml(object value) { if ((value == null) || (value == base.NullValue)) { return(string.Empty); } Type dataType = base.DataType; if ((dataType == typeof(byte[])) || ((dataType == typeof(object)) && (value is byte[]))) { return(Convert.ToBase64String((byte[])value)); } if ((dataType == typeof(Type)) || ((dataType == typeof(object)) && (value is Type))) { return(((Type)value).AssemblyQualifiedName); } if (!DataStorage.IsTypeCustomType(value.GetType())) { return((string)SqlConvert.ChangeTypeForXML(value, typeof(string))); } if (Type.GetTypeCode(value.GetType()) != TypeCode.Object) { return(value.ToString()); } StringWriter w = new StringWriter(base.FormatProvider); if (this.implementsIXmlSerializable) { using (XmlTextWriter writer2 = new XmlTextWriter(w)) { ((IXmlSerializable)value).WriteXml(writer2); } return(w.ToString()); } GetXmlSerializer(value.GetType()).Serialize((TextWriter)w, value); return(w.ToString()); }
public override object ConvertXmlToObject(XmlReader xmlReader, XmlRootAttribute xmlAttrib) { object?retValue = null; bool isBaseCLRType = false; bool legacyUDT = false; // in 1.0 and 1.1 we used to call ToString on CDT obj. so if we have the same case // we need to handle the case when we have column type as object. if (null == xmlAttrib) { // this means type implements IXmlSerializable Type? type = null; string?typeName = xmlReader.GetAttribute(Keywords.MSD_INSTANCETYPE, Keywords.MSDNS); if (typeName == null || typeName.Length == 0) { // No CDT polumorphism string?xsdTypeName = xmlReader.GetAttribute(Keywords.TYPE, Keywords.XSINS); // this xsd type: Base type polymorphism if (null != xsdTypeName && xsdTypeName.Length > 0) { string[] _typename = xsdTypeName.Split(':'); if (_typename.Length == 2) { // split will return aray of size 1 if ":" is not there if (xmlReader.LookupNamespace(_typename[0]) == Keywords.XSDNS) { xsdTypeName = _typename[1]; // trim the prefix and just continue with } } // for other case, let say we have two ':' in type, the we throws (as old behavior) type = XSDSchema.XsdtoClr(xsdTypeName); isBaseCLRType = true; } else if (_dataType == typeof(object)) { // there is no Keywords.MSD_INSTANCETYPE and no Keywords.TYPE legacyUDT = true; // see if our type is object } } if (legacyUDT) { // if Everett UDT, just read it and return string retValue = xmlReader.ReadString(); } else { if (typeName == Keywords.TYPEINSTANCE) { retValue = Type.GetType(xmlReader.ReadString()); xmlReader.Read(); // need to move to next node } else { if (null == type) { type = (typeName == null) ? _dataType : DataStorage.GetType(typeName); } if (type == typeof(char) || type == typeof(Guid)) { //msdata:char and msdata:guid imply base types. isBaseCLRType = true; } if (type == typeof(object)) { throw ExceptionBuilder.CanNotDeserializeObjectType(); } TypeLimiter.EnsureTypeIsAllowed(type); if (!isBaseCLRType) { retValue = System.Activator.CreateInstance(type, true) !; Debug.Assert(xmlReader is DataTextReader, "Invalid DataTextReader is being passed to customer"); ((IXmlSerializable)retValue).ReadXml(xmlReader); } else { // Process Base CLR type // for Element Node, if it is Empty, ReadString does not move to End Element; we need to move it if (type == typeof(string) && xmlReader.NodeType == XmlNodeType.Element && xmlReader.IsEmptyElement) { retValue = string.Empty; } else { retValue = xmlReader.ReadString(); if (type != typeof(byte[])) { retValue = SqlConvert.ChangeTypeForXML(retValue, type); } else { retValue = Convert.FromBase64String(retValue.ToString() !); } } xmlReader.Read(); } } } } else { XmlSerializer deserializerWithRootAttribute = ObjectStorage.GetXmlSerializer(_dataType, xmlAttrib); retValue = deserializerWithRootAttribute.Deserialize(xmlReader); } return(retValue !); }
public override object ConvertXmlToObject(XmlReader xmlReader, XmlRootAttribute xmlAttrib) { object obj2 = null; bool flag = false; bool flag2 = false; if (xmlAttrib != null) { return(GetXmlSerializer(base.DataType, xmlAttrib).Deserialize(xmlReader)); } Type type = null; string attribute = xmlReader.GetAttribute("InstanceType", "urn:schemas-microsoft-com:xml-msdata"); if ((attribute == null) || (attribute.Length == 0)) { string xsdTypeName = xmlReader.GetAttribute("type", "http://www.w3.org/2001/XMLSchema-instance"); if ((xsdTypeName != null) && (xsdTypeName.Length > 0)) { string[] strArray = xsdTypeName.Split(new char[] { ':' }); if ((strArray.Length == 2) && (xmlReader.LookupNamespace(strArray[0]) == "http://www.w3.org/2001/XMLSchema")) { xsdTypeName = strArray[1]; } type = XSDSchema.XsdtoClr(xsdTypeName); flag = true; } else if (base.DataType == typeof(object)) { flag2 = true; } } if (flag2) { return(xmlReader.ReadString()); } if (attribute == "Type") { obj2 = Type.GetType(xmlReader.ReadString()); xmlReader.Read(); return(obj2); } if (null == type) { type = (attribute == null) ? base.DataType : DataStorage.GetType(attribute); } if ((type == typeof(char)) || (type == typeof(Guid))) { flag = true; } if (type == typeof(object)) { throw ExceptionBuilder.CanNotDeserializeObjectType(); } if (!flag) { obj2 = Activator.CreateInstance(type, true); ((IXmlSerializable)obj2).ReadXml(xmlReader); return(obj2); } if (((type == typeof(string)) && (xmlReader.NodeType == XmlNodeType.Element)) && xmlReader.IsEmptyElement) { obj2 = string.Empty; } else { obj2 = xmlReader.ReadString(); if (type != typeof(byte[])) { obj2 = SqlConvert.ChangeTypeForXML(obj2, type); } else { obj2 = Convert.FromBase64String(obj2.ToString()); } } xmlReader.Read(); return(obj2); }