public IValue ReadXML(XmlReaderImpl xmlReader, IValue valueType = null) { TypeTypeValue typeValue = null; if (valueType is TypeTypeValue typeTypeValue) { typeValue = typeTypeValue; } else if (xmlReader.NodeType == xmlNodeEnum.FromNativeValue(XmlNodeType.Element)) { IValue xsiType = xmlReader.GetAttribute(ValueFactory.Create("type"), XmlSchema.InstanceNamespace); IValue xsiNil = xmlReader.GetAttribute(ValueFactory.Create("nil"), XmlSchema.InstanceNamespace); if (xsiType.DataType == DataType.String) { switch (xsiType.AsString()) { case "string": typeValue = new TypeTypeValue("String"); break; case "decimal": typeValue = new TypeTypeValue("Number"); break; case "boolean": typeValue = new TypeTypeValue("Boolean"); break; case "dateTime": typeValue = new TypeTypeValue("Date"); break; default: break; } } else if (xsiNil.DataType == DataType.String) { typeValue = new TypeTypeValue("Undefined"); } } ; if (typeValue == null) { throw RuntimeException.InvalidArgumentValue(); } Type implType = TypeManager.GetImplementingClass(typeValue.Value.ID); IValue result = ValueFactory.Create(); if (typeValue.Equals(new TypeTypeValue("Undefined"))) { result = ValueFactory.Create(); xmlReader.Skip(); } else if (implType == typeof(DataType)) { xmlReader.Read(); if (xmlReader.NodeType == xmlNodeEnum.FromNativeValue(XmlNodeType.Text)) { result = XMLValue(typeValue, xmlReader.Value); xmlReader.Read(); } else { throw RuntimeException.InvalidArgumentValue(); } } else if (typeof(IXDTOSerializableXML).IsAssignableFrom(implType)) { result = Activator.CreateInstance(implType, new object[] { xmlReader, this }) as IValue; } xmlReader.Read(); return(result); }
public IValue ReadXml(XmlReaderImpl reader, IXdtoType type = null) { if (reader.MoveToContent() == null) { // TODO: бросить исключение?? return(null); } if (type == null) { var explicitType = reader.GetAttribute(ValueFactory.Create("type"), XmlNs.xsi); if (explicitType.DataType == DataType.Undefined) { type = new XdtoObjectType(); } else { var defaultNamespace = reader.NamespaceContext.DefaultNamespace; // Задан тип - пытаемся его распознать var sType = explicitType.AsString(); var nameElements = sType.Split(':'); var typeUri = nameElements.Count() > 1 ? nameElements [0] : defaultNamespace; var typeName = nameElements.Count() > 1 ? nameElements [1] : nameElements [0]; var nsMapping = reader.NamespaceContext.LookupNamespaceUri(typeUri); if (nsMapping != null && nsMapping.DataType == DataType.String) { typeUri = nsMapping.AsString(); } type = this.Type(typeUri, typeName); } } if (type is XdtoObjectType) { return(ValueFactory.Create(type.Reader.ReadXml(reader, type, this))); } var xmlNodeTypeEnum = XmlNodeTypeEnum.CreateInstance(); var xmlElementStart = xmlNodeTypeEnum.FromNativeValue(XmlNodeType.Element); var xmlElementEnd = xmlNodeTypeEnum.FromNativeValue(XmlNodeType.EndElement); if (type is XdtoValueType) { if (reader.NodeType.Equals(xmlElementStart)) { reader.Read(); } var result = type.Reader.ReadXml(reader, type, this); if (!reader.NodeType.Equals(xmlElementEnd)) { reader.Skip(); } var pd = new PrimitiveValuesSerializer(); return(pd.DeserializeXdto(result)); } throw new NotSupportedException("Неожиданный тип XDTO!"); }