public IEnumerable<IMapping<XObject, IMemberContext>> MapForDeserialization(XElement source, IMemberContext target, IAdviceRequester adviceRequester) { if (source == null) throw new ArgumentNullException("source"); if (target == null) throw new ArgumentNullException("target"); var targetObject = target.GetValue(); foreach (var member in targetObject.Members.Where(m => !m.Attributes.OfType<XmlIgnoreAttribute>().Any())) { var sourceType = GetSourceTypeAndPotentialNames(member); switch (sourceType.Key) { case XmlNodeType.Element: foreach (var mapping in GetMappingsFromElement(sourceType.Value, source, targetObject, member, adviceRequester)) yield return mapping; break; case XmlNodeType.Attribute: yield return GetMappingFromAttribute(sourceType.Value, source, member); break; default: throw new ArgumentOutOfRangeException(); } } }
public bool Serialize(IMemberContext source, XObject target, IAdviceRequester adviceRequester) { if (source == null) throw new ArgumentNullException("source"); if (target.NodeType != XmlNodeType.Element) throw new ArgumentException("Parameter must be an XML element.", "target"); var elem = (XElement)target; IObjectContext sourceObject = source.GetValue(); if (sourceObject == null || sourceObject.GetObject() == null) { if (_isNullableStrategy.IsNullable(source, elem, adviceRequester)) { elem.Add(Constants.XsiNilAttribute); return true; } return false; } List<IMapping<IMemberContext, XObject>> mappings = _mapper.MapForSerialization(source, elem, adviceRequester) .ToList(); if (mappings == null) throw new ArgumentException("Unable to map source '" + source + "' and target '" + target + "'."); SerializeRecursively(elem, mappings, adviceRequester); return true; }
public bool Serialize(IMemberContext source, XObject target, IAdviceRequester adviceRequester) { if (source == null) throw new ArgumentNullException("source"); var sourceObject = source.GetValue().GetObject(); if (target.NodeType != XmlNodeType.Element) throw new ArgumentException("Parameter must be an XML element.", "source"); var elem = (XElement)target; var xmlSerializable = (IXmlSerializable)sourceObject; var memoryStream = new MemoryStream(); var xmlWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); xmlWriter.WriteStartElement(elem.Name.LocalName); xmlWriter.WriteAttributeString("xmlns", "xsi", null, Constants.XmlSchemaInstanceNamespaceAttribute.Value); xmlWriter.WriteAttributeString("xmlns", "xsd", null, Constants.XmlSchemaNamespaceAttribute.Value); xmlSerializable.WriteXml(xmlWriter); xmlWriter.WriteEndElement(); xmlWriter.Flush(); memoryStream.Position = 0; var tempTarget = XElement.Load(memoryStream); if (tempTarget.HasElements) foreach (var element in tempTarget.Elements()) elem.Add(element); else elem.SetValue(tempTarget.Value); foreach (var attribute in tempTarget.Attributes()) { if (attribute.Name == Constants.XmlSchemaNamespaceAttribute.Name || attribute.Name == Constants.XmlSchemaInstanceNamespaceAttribute.Name) continue; elem.Add(attribute); } foreach (var childElem in elem.Elements()) { var xmlnsXsiAttr = childElem.Attribute(Constants.XmlSchemaInstanceNamespaceAttribute.Name); if (xmlnsXsiAttr != null) xmlnsXsiAttr.Remove(); var xmlnsXsdAttr = childElem.Attribute(Constants.XmlSchemaNamespaceAttribute.Name); if (xmlnsXsdAttr != null) xmlnsXsdAttr.Remove(); } return true; }
public IEnumerable<IMapping<XObject, IMemberContext>> MapForDeserialization(XElement source, IMemberContext target, IAdviceRequester adviceRequester) { var targetObject = target.GetValue(); if (targetObject == null) throw new ArgumentException("Target should have had it value set to a collection at this point.", "target"); var elementNames = GetElementNames(target); return source.Elements() .Where(e => elementNames.Contains(e.Name, CaseInsensitiveXNameComparer.Instance)) .Select(elem => new Mapping<XObject, IMemberContext>(elem, targetObject.Members.Single())) .ToList(); }
public IEnumerable<IMapping<IMemberContext, XObject>> MapForSerialization(IMemberContext source, XElement target, IAdviceRequester adviceRequester) { if (source == null) throw new ArgumentNullException("source"); var elementType = GetElementType(source.GetActualType()); if (elementType == null) throw new ArgumentException("Collection mapper has been invoked for target that is not a known list type: '" + source.GetActualType() + "'.", "target"); var sourceObject = source.GetValue(); if (sourceObject == null) return Enumerable.Empty<IMapping<IMemberContext, XObject>>(); return sourceObject.Members .Select(CreateMapping); }
public IEnumerable<IMapping<IMemberContext, XObject>> MapForSerialization(IMemberContext source, XElement target, IAdviceRequester adviceRequester) { if (source == null) throw new ArgumentNullException("source"); if (target == null) throw new ArgumentNullException("target"); var sourceObject = source.GetValue(); if (sourceObject == null) return Enumerable.Empty<IMapping<IMemberContext, XObject>>(); return sourceObject.Members .Where(m => !m.Attributes.OfType<XmlIgnoreAttribute>().Any()) .Select(member => CreateMappingForSerialization(member, target)) .ToList(); }
private static object GetActualObject(IMemberContext memberContext) { var objectContextValue = memberContext.GetValue(); if (objectContextValue == null) return null; var objectContextValueObject = objectContextValue.GetObject(); return objectContextValueObject; }