/// <summary> /// This writes the public contents of the Entity in the properties element. /// </summary> /// <param name="entity">Entity</param> /// <returns>XElement representation of the properties element</returns> XElement WriteEntityContents(IOfflineEntity entity) { XElement contentElement = new XElement(FormatterConstants.ODataMetadataNamespace + FormatterConstants.PropertiesElementName); // Write only the primary keys if its an tombstone PropertyInfo[] properties = ReflectionUtility.GetPropertyInfoMapping(entity.GetType()); // Write individual properties to the feed, foreach (PropertyInfo fi in properties) { string edmType = FormatterUtilities.GetEdmType(fi.PropertyType); object value = fi.GetValue(entity, null); string valString = value as string; if (valString != null) { value = AtomHelper.CleanInvalidXmlChars(valString); } Type propType = fi.PropertyType; if (fi.PropertyType.IsGenericType && fi.PropertyType.Name.Equals(FormatterConstants.NullableTypeName, StringComparison.InvariantCulture)) { // Its a Nullable<T> property propType = fi.PropertyType.GetGenericArguments()[0]; } if (value == null) { contentElement.Add( new XElement(FormatterConstants.ODataDataNamespace + fi.Name, new XAttribute(FormatterConstants.ODataMetadataNamespace + FormatterConstants.AtomPubTypeElementName, edmType), new XAttribute(FormatterConstants.ODataMetadataNamespace + FormatterConstants.AtomPubIsNullElementName, true))); } else if (propType == FormatterConstants.DateTimeType || propType == FormatterConstants.TimeSpanType || propType == FormatterConstants.DateTimeOffsetType) { contentElement.Add( new XElement(FormatterConstants.ODataDataNamespace + fi.Name, new XAttribute(FormatterConstants.ODataMetadataNamespace + FormatterConstants.AtomPubTypeElementName, edmType), FormatterUtilities.ConvertDateTimeForType_Atom(value, propType))); } else if (propType != FormatterConstants.ByteArrayType) { contentElement.Add( new XElement(FormatterConstants.ODataDataNamespace + fi.Name, new XAttribute(FormatterConstants.ODataMetadataNamespace + FormatterConstants.AtomPubTypeElementName, edmType), value)); } else { byte[] bytes = (byte[])value; contentElement.Add( new XElement(FormatterConstants.ODataDataNamespace + fi.Name, new XAttribute(FormatterConstants.ODataMetadataNamespace + FormatterConstants.AtomPubTypeElementName, edmType), Convert.ToBase64String(bytes))); } } return(contentElement); }
/// <summary> /// Validates that the stream contains a valid feed item. /// </summary> public override void Start() { reader.MoveToContent(); if (!AtomHelper.IsAtomElement(reader, FormatterConstants.AtomPubFeedElementName)) { throw new InvalidOperationException("Not a valid ATOM feed."); } }
/// <summary> /// Traverses through the feed and returns when it arrives at the necessary element. /// </summary> /// <returns>bool detecting whether or not there is more elements to be read.</returns> public override bool Next() { // User did not read the node. if (currentType != ReaderItemType.BOF && !currentNodeRead) { reader.Skip(); } do { currentEntryWrapper = null; liveEntity = null; if (AtomHelper.IsAtomElement(reader, FormatterConstants.AtomPubEntryElementName) || AtomHelper.IsAtomTombstone(reader, FormatterConstants.AtomDeletedEntryElementName)) { currentType = ReaderItemType.Entry; currentNodeRead = false; return(true); } if (AtomHelper.IsSyncElement(reader, FormatterConstants.ServerBlobText)) { currentType = ReaderItemType.SyncBlob; currentNodeRead = false; return(true); } if (AtomHelper.IsSyncElement(reader, FormatterConstants.MoreChangesAvailableText)) { currentType = ReaderItemType.HasMoreChanges; currentNodeRead = false; return(true); } } while (reader.Read()); this.currentType = ReaderItemType.EOF; return(false); }