public void AddEntry(SyndicationItemFormatter item) { WebOperationContext context = WebOperationContext.Current; // Add entry // .. context.OutgoingResponse.StatusCode = HttpStatusCode.Created; context.OutgoingResponse.StatusDescription = String.Format("Syndication item added with title '{0}'", item.Item.Title.Text); }
private static void ReadWriteSyndicationItem(string file, Func <SyndicationItem, SyndicationItemFormatter> itemFormatter) { string serializeFilePath = Path.GetTempFileName(); bool toDeletedFile = true; try { SyndicationItem itemObjct = null; using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read)) { using (XmlReader reader = XmlDictionaryReader.CreateTextReader(fileStream, XmlDictionaryReaderQuotas.Max)) { itemObjct = SyndicationItem.Load(reader); } } using (FileStream fileStream = new FileStream(serializeFilePath, FileMode.OpenOrCreate)) { using (XmlWriter writer = XmlDictionaryWriter.CreateTextWriter(fileStream)) { SyndicationItemFormatter formatter = itemFormatter(itemObjct); formatter.WriteTo(writer); } } // compare file filePath and serializeFilePath CompareHelper ch = new CompareHelper { Diff = new XmlDiff() { Option = XmlDiffOption.IgnoreComments | XmlDiffOption.IgnorePrefix | XmlDiffOption.IgnoreWhitespace | XmlDiffOption.IgnoreChildOrder | XmlDiffOption.IgnoreAttributeOrder } }; string diffNode = string.Empty; if (!ch.Compare(file, serializeFilePath, out diffNode)) { toDeletedFile = false; string errorMessage = $"The generated file was different from the baseline file:{Environment.NewLine}Baseline: {file}{Environment.NewLine}Actual: {serializeFilePath}{Environment.NewLine}Different Nodes:{Environment.NewLine}{diffNode}"; Assert.True(false, errorMessage); } } catch (Exception e) { Exception newEx = new Exception($"Failed File Name: {file}", e); throw newEx; } finally { if (toDeletedFile) { File.Delete(serializeFilePath); } } }
/// <summary>Reads a SyndicationItem object from the specified XmlReader.</summary> /// <param name='formatter'>Formatter to use when reading content.</param> /// <param name='reader'>Read to read feed from.</param> /// <returns>A new SyndicationItem instance.</returns> private static SyndicationItem ReadSyndicationItem(SyndicationItemFormatter formatter, XmlReader reader) { Debug.Assert(formatter != null, "formatter != null"); Debug.Assert(reader != null, "reader != null"); try { formatter.ReadFrom(reader); } catch (XmlException exception) { throw DataServiceException.CreateBadRequestError(Strings.Syndication_ErrorReadingEntry(exception.Message), exception); } Debug.Assert(formatter.Item != null, "formatter.Item != null"); return(formatter.Item); }
/// <summary> /// Gets a formatter for syndicating items based on the input media type. /// </summary> /// <param name="contentType">The content media type.</param> /// <param name="item">The syndication item to format.</param> /// <returns> /// Returns either an <see cref="T:System.ServiceModel.Syndication.Atom10ItemFormmater"/> or /// a <see cref="T:System.ServiceModel.Syndication.Rss20ItemFormatter"/>. /// </returns> /// <exception cref="T:System.NotSupportedException">if the <paramref name="contentType"/> /// is not a valid Atom or RSS media type.</exception> protected SyndicationItemFormatter GetFormatter(string contentType, SyndicationItem item = null) { Throw.IfArgumentNullOrEmpty(contentType, "contentType"); Throw.IfArgumentNull(item, "item"); SyndicationItemFormatter formatter = null; if (contentType.ToLower() == AtomMediaType) { formatter = item == null ? new Atom10ItemFormatter() : new Atom10ItemFormatter(item); } else if (contentType.ToLower() == RssMediaType) { formatter = item == null ? new Rss20ItemFormatter() : new Rss20ItemFormatter(item); } else { Throw.NotSupported(string.Format("Content type '{0}' is not supported.", contentType)); } return(formatter); }