public override object ReadFromStream(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger) { HttpContentHeaders contentHeaders = content == null ? null : content.Headers; // If content length is 0 then return default value for this type if (contentHeaders != null && contentHeaders.ContentLength == null) { return(GetDefaultValueForType(type)); } try { using (readStream) { using (var reader = XmlReader.Create(readStream)) { var formatter = new Atom10ItemFormatter(); formatter.ReadFrom(reader); var command = Activator.CreateInstance(type); ((IPublicationCommand)command).ReadSyndicationItem(formatter.Item); return(command); } } } catch (Exception e) { if (formatterLogger == null) { throw; } formatterLogger.LogError(String.Empty, e); return(GetDefaultValueForType(type)); } }
public static void AtomEntryPositiveTest() { string filePath = @"brief-entry-noerror.xml"; string serializeFilePath = Path.GetTempFileName(); try { SyndicationItem feedObjct = null; using (XmlReader reader = XmlReader.Create(filePath)) { feedObjct = SyndicationItem.Load(reader); reader.Close(); } using (XmlWriter writer = XmlWriter.Create(serializeFilePath)) { Atom10ItemFormatter atomformatter = new Atom10ItemFormatter(feedObjct); atomformatter.WriteTo(writer); writer.Close(); } // compare file filePath and serializeFilePath XmlDiff diff = new XmlDiff() { Option = XmlDiffOption.IgnoreComments | XmlDiffOption.IgnorePrefix | XmlDiffOption.IgnoreWhitespace | XmlDiffOption.IgnoreChildOrder | XmlDiffOption.IgnoreAttributeOrder }; Assert.True(diff.Compare(filePath, serializeFilePath)); } finally { File.Delete(serializeFilePath); } }
public static void AtomEntryPositiveTest_write() { string filePath = @"AtomEntryTest.xml"; string serializeFilePath = Path.GetTempFileName(); SyndicationItem item = new SyndicationItem("SyndicationFeed released for .net Core", "A lot of text describing the release of .net core feature", new Uri("http://contoso.com/news/path")); item.Id = "uuid:43481a10-d881-40d1-adf2-99b438c57e21;id=1"; item.LastUpdatedTime = new DateTimeOffset(Convert.ToDateTime("2017-10-11T11:25:55Z")).UtcDateTime; try { using (XmlWriter writer = XmlWriter.Create(serializeFilePath)) { Atom10ItemFormatter f = new Atom10ItemFormatter(item); f.WriteTo(writer); writer.Close(); } XmlDiff diff = new XmlDiff() { Option = XmlDiffOption.IgnoreComments | XmlDiffOption.IgnorePrefix | XmlDiffOption.IgnoreWhitespace | XmlDiffOption.IgnoreChildOrder | XmlDiffOption.IgnoreAttributeOrder }; Assert.True(diff.Compare(filePath, serializeFilePath)); } finally { File.Delete(serializeFilePath); } }
public void ReadFrom_Extension() { Atom10ItemFormatter f = new Atom10ItemFormatter(); f.ReadFrom(CreateReader("<entry xmlns='http://www.w3.org/2005/Atom'><ext>external</ext></entry>")); Assert.IsNotNull(f.Item, "#1"); Assert.AreEqual(1, f.Item.ElementExtensions.Count, "#2"); }
public void ReadFrom_Id() { Atom10ItemFormatter f = new Atom10ItemFormatter(); f.ReadFrom(CreateReader("<entry xmlns='http://www.w3.org/2005/Atom'><id>urn:myid</id></entry>")); Assert.IsNotNull(f.Item, "#1"); Assert.AreEqual("urn:myid", f.Item.Id, "#2"); }
public T DeserializeResourceDescription <T>(string resourceDescription) { DataContractSerializer serializer = new DataContractSerializer(typeof(T)); Atom10ItemFormatter formater = new Atom10ItemFormatter(); formater.ReadFrom(new XmlTextReader(new StringReader(resourceDescription))); XmlSyndicationContent content = formater.Item.Content as XmlSyndicationContent; return(content.ReadContent <T>(serializer)); }
private void WriteAtomEntry(IPublication publication, Stream writeStream) { var entry = publication.Syndicate(); var formatter = new Atom10ItemFormatter(entry); using (var writer = XmlWriter.Create(writeStream)) { formatter.WriteTo(writer); } }
public static void Snippet15() { // <Snippet15> Atom10ItemFormatter myItemAtomFormatter = new Atom10ItemFormatter(typeof(MySyndicationItem)); XmlReader atomReader = XmlReader.Create("http://Contoso/Feeds/MyFeed/Items"); myItemAtomFormatter.ReadFrom(atomReader); atomReader.Close(); // </Snippet15> }
public void ReadXml_TitleOnly() { Atom10ItemFormatter f = new Atom10ItemFormatter(); ((IXmlSerializable)f).ReadXml(CreateReader("<entry xmlns='http://www.w3.org/2005/Atom'><title>test</title></entry>")); Assert.IsNotNull(f.Item.Title, "#1"); Assert.AreEqual("test", f.Item.Title.Text, "#2"); ((IXmlSerializable)f).ReadXml(CreateReader("<dummy><title>test</title></dummy>")); // it is ok }
public static void Snippet42() { // <Snippet42> SyndicationItem item = new SyndicationItem("Item Title", "Item Content", new Uri("http://Item/Alternate/Link"), "itemID", DateTimeOffset.Now); Atom10ItemFormatter atomFormatter = item.GetAtom10Formatter(); XmlWriter writer = XmlWriter.Create("output.xml"); atomFormatter.WriteTo(writer); writer.Close(); // </Snippet42> }
public static void Snippet14() { // <Snippet14> SyndicationItem item = new SyndicationItem("Test Item", "This is the content for Test Item", new Uri("http://localhost/ItemOne"), "TestItemID", DateTime.Now); Atom10ItemFormatter atomItemFormatter = new Atom10ItemFormatter(item); XmlWriter atomWriter = XmlWriter.Create("Atom.xml"); atomItemFormatter.WriteTo(atomWriter); atomWriter.Close(); // </Snippet14> }
public async Task <Stream> GetStreamAsync() { return(await Task <Stream> .Run(() => { MemoryStream ms = new MemoryStream(); var sw = XmlWriter.Create(ms); Atom10ItemFormatter atomFormatter = new Atom10ItemFormatter(item); atomFormatter.WriteTo(sw); sw.Flush(); ms.Seek(0, SeekOrigin.Begin); return ms as Stream; })); }
// </Snippet10> // <Snippet11> public void SerializeItem(SyndicationItem item) { Atom10ItemFormatter atomFormatter = item.GetAtom10Formatter(); Rss20ItemFormatter rssFormatter = item.GetRss20Formatter(); XmlWriter atomWriter = XmlWriter.Create("atom.xml"); XmlWriter rssWriter = XmlWriter.Create("rss.xml"); atomFormatter.WriteTo(atomWriter); rssFormatter.WriteTo(rssWriter); atomWriter.Close(); rssWriter.Close(); }
public static void Snippet13() { // <Snippet13> Atom10ItemFormatter itemFormatter = new Atom10ItemFormatter(); XmlReader atomReader = XmlReader.Create("http://Contoso/Feeds/MyFeed/Item"); if (itemFormatter.CanRead(atomReader)) { itemFormatter.ReadFrom(atomReader); atomReader.Close(); } // </Snippet13> }
public void ReadFrom1() { Atom10ItemFormatter f = new Atom10ItemFormatter(); Assert.IsNull(f.Item, "#1"); f.ReadFrom(CreateReader("<entry xmlns='http://www.w3.org/2005/Atom'><title>test</title></entry>")); SyndicationItem item1 = f.Item; Assert.IsNotNull(f.Item.Title, "#2"); Assert.AreEqual("test", f.Item.Title.Text, "#3"); f.ReadFrom(CreateReader("<entry xmlns='http://www.w3.org/2005/Atom'><title>test</title></entry>")); Assert.IsFalse(object.ReferenceEquals(item1, f.Item), "#4"); }
public void ReadFrom_Link() { Atom10ItemFormatter f = new Atom10ItemFormatter(); f.ReadFrom(CreateReader("<entry xmlns='http://www.w3.org/2005/Atom'><link href='urn:foo' rel='enclosure' length='50' type='text/html' wcf='wtf'><extended /></link></entry>")); Assert.AreEqual(1, f.Item.Links.Count, "#1"); SyndicationLink link = f.Item.Links [0]; Assert.AreEqual(50, link.Length, "#2"); Assert.AreEqual("urn:foo", link.Uri.ToString(), "#3"); Assert.AreEqual("text/html", link.MediaType, "#4"); Assert.AreEqual("enclosure", link.RelationshipType, "#5"); Assert.AreEqual(1, link.AttributeExtensions.Count, "#6"); Assert.AreEqual(1, link.ElementExtensions.Count, "#7"); }
public static void AtomEntryPositiveTest_write() { string file = @"AtomEntryTest.xml"; string serializeFilePath = Path.GetTempFileName(); bool toDeletedFile = true; SyndicationItem item = new SyndicationItem("SyndicationFeed released for .net Core", "A lot of text describing the release of .net core feature", new Uri("http://contoso.com/news/path")); item.Id = "uuid:43481a10-d881-40d1-adf2-99b438c57e21;id=1"; item.LastUpdatedTime = new DateTimeOffset(Convert.ToDateTime("2017-10-11T11:25:55Z")).UtcDateTime; try { using (FileStream fileStream = new FileStream(serializeFilePath, FileMode.OpenOrCreate)) { using (XmlWriter writer = XmlDictionaryWriter.CreateTextWriter(fileStream)) { Atom10ItemFormatter f = new Atom10ItemFormatter(item); f.WriteTo(writer); } } 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); } } finally { if (toDeletedFile) { File.Delete(serializeFilePath); } } }
public static void Snippet9() { // <Snippet9> // <Snippet10> MySyndicationItem item = new MySyndicationItem("Test Item", "This is the content for Test Item", new Uri("http://localhost/ItemOne"), "TestItemID", DateTime.Now); item.Links.Add(new SyndicationLink(new Uri("http://someuri"), "alternate", "MyItemLink", "text/html", 100)); item.PublishDate = new DateTime(1968, 2, 23); item.LastUpdatedTime = DateTime.Today; item.SourceFeed = new SyndicationFeed(); item.Summary = new TextSyndicationContent("This the item summary"); XmlWriter atomWriter = XmlWriter.Create("AtomItem.xml"); Atom10ItemFormatter <MySyndicationItem> formatter = new Atom10ItemFormatter <MySyndicationItem>(item); // </Snippet10> formatter.WriteTo(atomWriter); atomWriter.Close(); // </Snippet9> }
public void CanRead() { Atom10ItemFormatter f = new Atom10ItemFormatter(); Assert.IsFalse(f.CanRead(CreateReader("<feed xmlns='http://www.w3.org/2005/Atom'>")), "#1"); Assert.IsTrue(f.CanRead(CreateReader("<entry xmlns='http://www.w3.org/2005/Atom'>")), "#2"); Assert.IsFalse(f.CanRead(CreateReader("<entry>")), "#3"); Assert.IsFalse(f.CanRead(CreateReader("<item>")), "#4"); Assert.IsFalse(f.CanRead(CreateReader("<hoge xmlns='http://www.w3.org/2005/Atom'>")), "#5"); XmlReader r = CreateReader("<entry xmlns='http://www.w3.org/2005/Atom'></entry>"); r.Read(); // element r.Read(); // endelement Assert.IsFalse(f.CanRead(r), "#6"); r = CreateReader("<entry xmlns='http://www.w3.org/2005/Atom'><title>test</title></entry>"); r.Read(); // item r.Read(); // title Assert.IsFalse(f.CanRead(r), "#7"); }
/// <summary> /// Creates the atom entry document. /// </summary> /// <param name="atomXmlReader">The atom XML reader.</param> private void CreateAtomEntryDocument(XmlReader atomXmlReader) { Atom10ItemFormatter formatter = new Atom10ItemFormatter(); bool isValidAtomEntry = formatter.CanRead(atomXmlReader); if (isValidAtomEntry) { AtomDocument = SyndicationItem.Load(atomXmlReader); bool isValidAtomPubEntry = null != AtomDocument.Id && DateTimeOffset.MinValue != AtomDocument.LastUpdatedTime && null != AtomDocument.Title && null != AtomDocument.Authors && 0 < AtomDocument.Authors.Count; if (isValidAtomPubEntry) { if (null == AtomDocument.Content) { isValidAtomPubEntry = AtomDocument.Links.Where(atomLink => string.Compare(atomLink.RelationshipType, "alternate", StringComparison.OrdinalIgnoreCase) == 0) .Count() == 1; } else if (!(AtomDocument.Content is TextSyndicationContent)) { isValidAtomPubEntry = null != AtomDocument.Summary; } } if (!isValidAtomPubEntry) { throw new ArgumentException(Resources.ATOMPUB_INVALID_ATOMENTRYDOCUMENT, "atomXmlReader"); } else { SetProperties(); } } else { throw new ArgumentException(Resources.ATOMPUB_INVALID_ATOMENTRYDOCUMENT, "atomXmlReader"); } }
private Stream GetEntryStream() { XmlWriter writer = null; MemoryStream entryStream = new MemoryStream(); try { writer = XmlWriter.Create(entryStream); Atom10ItemFormatter atomFormatter = AtomDocument.GetAtom10Formatter(); atomFormatter.WriteTo(writer); writer.Close(); entryStream.Seek(0, SeekOrigin.Begin); } finally { if (null != writer) { writer.Close(); } } return(entryStream); }
public void Version() { Atom10ItemFormatter f = new Atom10ItemFormatter(); Assert.AreEqual("Atom10", f.Version, "#1"); }
public void ReadFrom_WrongDate() { Atom10ItemFormatter f = new Atom10ItemFormatter(); f.ReadFrom(CreateReader("<entry xmlns='http://www.w3.org/2005/Atom'><published>Sat, 01 Jan 2000 00:00:00 Z</pubDate></entry>")); }
// It is not rejected. Though I think it is .NET bug. public void ReadFrom_EmptyDate() { Atom10ItemFormatter f = new Atom10ItemFormatter(); f.ReadFrom(CreateReader("<entry xmlns='http://www.w3.org/2005/Atom'><pubDate /></entry>")); }