public object Deserialize(ControllerContext controllerContext, ModelBindingContext bindingContext, ContentType requestFormat) { if (!CanDeserialize(requestFormat)) { throw new NotSupportedException(); } // We assume an Atom entry is being posted - since a feed is never posted in this example. using (XmlReader reader = XmlReader.Create(controllerContext.HttpContext.Request.InputStream, new XmlReaderSettings() { IgnoreWhitespace = true, IgnoreComments = true })) { SyndicationItem entry = SyndicationItem.Load(reader); XmlSyndicationContent xml = entry.Content as XmlSyndicationContent; if (xml == null) { throw new InvalidOperationException("Xml content expected in Atom entry"); } DataContractSerializer serializer = new DataContractSerializer(bindingContext.ModelType); using (XmlDictionaryReader innerReader = xml.GetReaderAtContent()) { innerReader.ReadStartElement(); return(serializer.ReadObject(innerReader, false)); } } }
public void GetReaderAtContent_ObjectWithXmlSerializer_ReturnsExpected() { var extensionObject = new ExtensionObject() { Value = 10 }; var content = new XmlSyndicationContent("type", extensionObject, new XmlSerializer(typeof(ExtensionObject))); using (XmlReader reader = content.GetReaderAtContent()) { CompareHelper.AssertEqualLongString(@"<content type=""type"" xmlns=""http://www.w3.org/2005/Atom""><ExtensionObject xmlns="""" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><Value>10</Value></ExtensionObject></content>", reader.ReadOuterXml()); } }
public void GetReaderAtContent_ObjectWithXmlObjectSerializer_ReturnsExpected() { var extensionObject = new ExtensionObject() { Value = 10 }; var content = new XmlSyndicationContent("type", extensionObject, new DataContractSerializer(typeof(ExtensionObject))); using (XmlReader reader = content.GetReaderAtContent()) { CompareHelper.AssertEqualLongString(@"<content type=""type"" xmlns=""http://www.w3.org/2005/Atom""><XmlSyndicationContentTests.ExtensionObject xmlns=""http://schemas.datacontract.org/2004/07/System.ServiceModel.Syndication.Tests"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><Value>10</Value></XmlSyndicationContentTests.ExtensionObject></content>", reader.ReadOuterXml()); } }
public void GetReaderAtContent_WithReader_ReturnsExpected() { var content = new XmlSyndicationContent( new XElement("ParentObject", new XElement("ExtensionObject", new XElement("Value", 10) ) ).CreateReader() ); XmlReader reader = content.GetReaderAtContent(); CompareHelper.AssertEqualLongString(@"<ParentObject><ExtensionObject><Value>10</Value></ExtensionObject></ParentObject>", reader.ReadOuterXml()); }
/// <summary> /// Gets the XmlReader for the m:properties element under the atom:content element /// </summary> /// <param name="item">item to read from</param> /// <returns>XmlReader for the m:properties element if found, null otherwise.</returns> private static XmlReader GetPropertiesReaderFromContent(SyndicationItem item) { XmlSyndicationContent itemContent = item.Content as XmlSyndicationContent; XmlReader reader = null; if (itemContent != null) { string contentType = itemContent.Type; if (!WebUtil.CompareMimeType(contentType, XmlConstants.MimeApplicationXml)) { throw DataServiceException.CreateBadRequestError( Strings.Syndication_EntryContentTypeUnsupported(contentType)); } bool shouldDispose = false; try { reader = itemContent.GetReaderAtContent(); WebUtil.XmlReaderEnsureElement(reader); Debug.Assert( reader.NodeType == XmlNodeType.Element, reader.NodeType.ToString() + " == XmlNodeType.Element -- otherwise XmlSyndicationContent didn't see a 'content' tag"); reader.ReadStartElement(XmlConstants.AtomContentElementName, XmlConstants.AtomNamespace); if (!reader.IsStartElement(XmlConstants.AtomPropertiesElementName, XmlConstants.DataWebMetadataNamespace)) { shouldDispose = true; } } catch { shouldDispose = true; throw; } finally { if (shouldDispose) { WebUtil.Dispose(reader); reader = null; } } } return(reader); }
/// <summary> /// Find the m:properties element within a feed entry /// </summary> /// <param name="item">The feed entry</param> /// <returns>The m:properties element</returns> private static XElement GetPropertiesElement(SyndicationItem item) { // Check if the m:properties element is within the content element XmlSyndicationContent xmlContent = item.Content as XmlSyndicationContent; if (xmlContent != null) { XElement contentElement = XElement.Load(xmlContent.GetReaderAtContent()); return(contentElement.Elements().FirstOrDefault(e => e.Name == XName.Get("properties", odataMetaXmlNs))); } // If we're here, then we are dealing with a feed that has an MLE // i.e. the m:properties element is a peer of the content element, and shows up // in the elementExtensions instead SyndicationElementExtension propertiesElementExtension = item.ElementExtensions.FirstOrDefault(e => e.OuterName.Equals("properties")); if (propertiesElementExtension != null) { XNode propertiesElement = XNode.ReadFrom(propertiesElementExtension.GetReader()); return((XElement)propertiesElement); } throw new NotSupportedException("Unsupported feed entry format"); }
public void GetReaderAtContent() { var x = new SyndicationElementExtension(6); // premise. Assert.AreEqual("<int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">6</int>", x.GetReader().ReadOuterXml(), "#1"); var t = new XmlSyndicationContent("text/xml", 6, (XmlObjectSerializer)null); Assert.AreEqual("<content type=\"text/xml\" xmlns=\"http://www.w3.org/2005/Atom\"><int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">6</int></content>", t.GetReaderAtContent().ReadOuterXml(), "#2"); }
public void GetReaderAtContent () { var x = new SyndicationElementExtension (6); // premise. Assert.AreEqual ("<int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">6</int>", x.GetReader ().ReadOuterXml (), "#1"); var t = new XmlSyndicationContent ("text/xml", 6, (XmlObjectSerializer) null); Assert.AreEqual ("<content type=\"text/xml\" xmlns=\"http://www.w3.org/2005/Atom\"><int xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">6</int></content>", t.GetReaderAtContent ().ReadOuterXml (), "#2"); }
/// <summary> /// Updates the resource property. /// </summary> /// <param name="context">The context.</param> /// <param name="resource">The resource.</param> /// <param name="atomEntry">The atom entry.</param> private static void UpdateResourceProperty( ZentityContext context, ScholarlyWork resource, AtomEntryDocument atomEntry) { resource.Title = atomEntry.Title.Text; SetContentType(context, resource, atomEntry.Title.Type, AtomPubConstants.TitleTypeProperty); resource.DateModified = DateTime.Now; Publication publication = resource as Publication; if (null != publication && atomEntry.PublishDate != DateTimeOffset.MinValue) { publication.DatePublished = atomEntry.PublishDate.DateTime; } if (null != atomEntry.Copyright) { resource.Copyright = atomEntry.Copyright.Text; SetContentType(context, resource, atomEntry.Copyright.Type, AtomPubConstants.CopyrightTypeProperty); } if (null != atomEntry.Summary && !string.IsNullOrEmpty(atomEntry.Summary.Text)) { SetExtensionProperty(context, resource, AtomPubConstants.SummaryProperty, atomEntry.Summary.Text); SetContentType(context, resource, atomEntry.Summary.Type, AtomPubConstants.SummaryTypeProperty); } if (null != atomEntry.Content) { UrlSyndicationContent urlContent = atomEntry.Content as UrlSyndicationContent; if (null != urlContent) { resource.Description = null; SetExtensionProperty(context, resource, AtomPubConstants.ContentUrlProperty, urlContent.Url.AbsoluteUri); } else { ResourceProperty urlContentProperty = ZentityAtomPubStoreReader.GetResourceProperty(resource, AtomPubConstants.ContentUrlProperty); if (null != urlContentProperty) { resource.ResourceProperties.Remove(urlContentProperty); } TextSyndicationContent textDescription = atomEntry.Content as TextSyndicationContent; if (null != textDescription) { resource.Description = textDescription.Text; } else { XmlSyndicationContent content = atomEntry.Content as XmlSyndicationContent; if (null != content) { XmlDictionaryReader contentReader = content.GetReaderAtContent(); StringBuilder contentValue = new StringBuilder(151); try { while (contentReader.Read()) { contentValue.Append(contentReader.Value); } } finally { contentReader.Close(); } resource.Description = contentValue.ToString(); } } } SetContentType(context, resource, atomEntry.Content.Type, AtomPubConstants.DescriptionTypeProperty); } if (null != atomEntry.Source) { ResourceProperty source = ZentityAtomPubStoreReader.GetResourceProperty(resource, AtomPubConstants.SourceProperty); if (null == source) { Property sourceProperty = GetProperty(context, AtomPubConstants.SourceProperty); source = new ResourceProperty { Property = sourceProperty, Resource = resource, }; } source.Value = atomEntry.Source; } #region Add Links List <ResourceProperty> links = ZentityAtomPubStoreReader.GetResourceProperties(resource, AtomPubConstants.LinksProperty); if (0 == atomEntry.XmlLinks.Count && null != links) { foreach (var item in links) { resource.ResourceProperties.Remove(item); } } Property linkProperty = GetProperty(context, AtomPubConstants.LinksProperty); foreach (string xmlLink in atomEntry.XmlLinks) { resource.ResourceProperties.Add(new ResourceProperty { Resource = resource, Property = linkProperty, Value = xmlLink }); } #endregion var authors = atomEntry.Authors.Select(author => new Person { Title = author.Name, Email = author.Email, Uri = author.Uri }); // Bug Fix : 177689 - AtomPub (M2): Author node is appending after every PUT request instead // of overwriting it. // Remove previous authors. resource.Authors.Clear(); AddPersons(context, resource.Authors, authors); resource.Contributors.Clear(); var contributors = atomEntry.Contributors.Select(author => new Person { Title = author.Name, Email = author.Email, Uri = author.Uri }); AddPersons(context, resource.Contributors, contributors); }