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(); } 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); }
internal void LoadTypeValues(XmlSchemaSimpleType node) { if ((node.Content is XmlSchemaSimpleTypeList) || (node.Content is XmlSchemaSimpleTypeUnion)) { throw ExceptionBuilder.SimpleTypeNotSupported(); } if (node.Content is XmlSchemaSimpleTypeRestriction) { XmlSchemaSimpleTypeRestriction content = (XmlSchemaSimpleTypeRestriction)node.Content; XmlSchemaSimpleType ancestor = node.BaseXmlSchemaType as XmlSchemaSimpleType; if ((ancestor != null) && (ancestor.QualifiedName.Namespace != Keywords.XSDNS)) // I'm assuming that built-in types don't have a name! // Console.WriteLine("In simpleNode, ancestor.Name = '{0}'", ancestor.Name); { baseSimpleType = new SimpleType(node.BaseXmlSchemaType as XmlSchemaSimpleType); // baseSimpleType = new SimpleType(node); } // do we need to put qualified name? // for user defined simpletype, always go with qname if (content.BaseTypeName.Namespace == Keywords.XSDNS) { baseType = content.BaseTypeName.Name; } else { baseType = content.BaseTypeName.ToString(); } if (baseSimpleType != null && baseSimpleType.Name != null && baseSimpleType.Name.Length > 0) { xmlBaseType = baseSimpleType.XmlBaseType;// SimpleTypeQualifiedName; } else { xmlBaseType = content.BaseTypeName; } if (baseType == null || baseType.Length == 0) { // Console.WriteLine("baseType == null, setting it to ", content.BaseType.Name); baseType = content.BaseType.Name; xmlBaseType = null; } if (baseType == "NOTATION") { baseType = "string"; } foreach (XmlSchemaFacet facet in content.Facets) { if (facet is XmlSchemaLengthFacet) { length = Convert.ToInt32(facet.Value, null); } if (facet is XmlSchemaMinLengthFacet) { minLength = Convert.ToInt32(facet.Value, null); } if (facet is XmlSchemaMaxLengthFacet) { maxLength = Convert.ToInt32(facet.Value, null); } if (facet is XmlSchemaPatternFacet) { pattern = facet.Value; } if (facet is XmlSchemaEnumerationFacet) { enumeration = !Common.ADP.IsEmpty(enumeration) ? enumeration + " " + facet.Value : facet.Value; } if (facet is XmlSchemaMinExclusiveFacet) { minExclusive = facet.Value; } if (facet is XmlSchemaMinInclusiveFacet) { minInclusive = facet.Value; } if (facet is XmlSchemaMaxExclusiveFacet) { maxExclusive = facet.Value; } if (facet is XmlSchemaMaxInclusiveFacet) { maxInclusive = facet.Value; } } } string tempStr = XSDSchema.GetMsdataAttribute(node, Keywords.TARGETNAMESPACE); if (tempStr != null) { ns = tempStr; } }
internal void LoadTypeValues(XmlSchemaSimpleType node) { if ((node.Content is XmlSchemaSimpleTypeList) || (node.Content is XmlSchemaSimpleTypeUnion)) { throw ExceptionBuilder.SimpleTypeNotSupported(); } if (node.Content is XmlSchemaSimpleTypeRestriction) { XmlSchemaSimpleTypeRestriction content = (XmlSchemaSimpleTypeRestriction)node.Content; XmlSchemaSimpleType ancestor = node.BaseXmlSchemaType as XmlSchemaSimpleType; if ((ancestor != null) && (ancestor.QualifiedName.Namespace != Keywords.XSDNS)) { _baseSimpleType = new SimpleType(node.BaseXmlSchemaType as XmlSchemaSimpleType); } // do we need to put qualified name? // for user defined simpletype, always go with qname if (content.BaseTypeName.Namespace == Keywords.XSDNS) { _baseType = content.BaseTypeName.Name; } else { _baseType = content.BaseTypeName.ToString(); } if (_baseSimpleType != null && _baseSimpleType.Name != null && _baseSimpleType.Name.Length > 0) { _xmlBaseType = _baseSimpleType.XmlBaseType;// SimpleTypeQualifiedName; } else { _xmlBaseType = content.BaseTypeName; } if (_baseType == null || _baseType.Length == 0) { _baseType = content.BaseType.Name; _xmlBaseType = null; } if (_baseType == "NOTATION") { _baseType = "string"; } foreach (XmlSchemaFacet facet in content.Facets) { if (facet is XmlSchemaLengthFacet) { _length = Convert.ToInt32(facet.Value, null); } if (facet is XmlSchemaMinLengthFacet) { _minLength = Convert.ToInt32(facet.Value, null); } if (facet is XmlSchemaMaxLengthFacet) { _maxLength = Convert.ToInt32(facet.Value, null); } if (facet is XmlSchemaPatternFacet) { _pattern = facet.Value; } if (facet is XmlSchemaEnumerationFacet) { _enumeration = !string.IsNullOrEmpty(_enumeration) ? _enumeration + " " + facet.Value : facet.Value; } if (facet is XmlSchemaMinExclusiveFacet) { _minExclusive = facet.Value; } if (facet is XmlSchemaMinInclusiveFacet) { _minInclusive = facet.Value; } if (facet is XmlSchemaMaxExclusiveFacet) { _maxExclusive = facet.Value; } if (facet is XmlSchemaMaxInclusiveFacet) { _maxInclusive = facet.Value; } } } string tempStr = XSDSchema.GetMsdataAttribute(node, Keywords.TARGETNAMESPACE); if (tempStr != null) { _ns = tempStr; } }
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); }