/// <summary> /// Saves the syndication resource to the specified <see cref="XmlWriter"/> and <see cref="SyndicationResourceSaveSettings"/>. /// </summary> /// <param name="writer">The <b>XmlWriter</b> to which you want to save the syndication resource.</param> /// <param name="settings">The <see cref="SyndicationResourceSaveSettings"/> object used to configure the persistance of the <see cref="AtomEntry"/> instance.</param> /// <exception cref="ArgumentNullException">The <paramref name="writer"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException">The <paramref name="settings"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="XmlException">The operation would not result in well formed XML for the syndication resource.</exception> public new void Save(XmlWriter writer, SyndicationResourceSaveSettings settings) { Guard.ArgumentNotNull(writer, "writer"); Guard.ArgumentNotNull(settings, "settings"); List <ISyndicationExtension> list = new List <ISyndicationExtension>(this.Extensions); if (this.EditedOn != DateTime.MinValue) { if (!list.Exists(AtomPublishingEditedSyndicationExtension.MatchByType)) { AtomPublishingEditedSyndicationExtension editedExtension = new AtomPublishingEditedSyndicationExtension(); editedExtension.Context.EditedOn = this.EditedOn; this.AddExtension(editedExtension); } } if (this.IsDraft) { if (!list.Exists(AtomPublishingControlSyndicationExtension.MatchByType)) { AtomPublishingControlSyndicationExtension controlExtension = new AtomPublishingControlSyndicationExtension(); controlExtension.Context.IsDraft = this.IsDraft; this.AddExtension(controlExtension); } } base.Save(writer, settings); }
/// <summary> /// Loads the syndication resource from the supplied <see cref="Uri"/> using the specified <see cref="ICredentials">credentials</see>, <see cref="IWebProxy">proxy</see> and <see cref="SyndicationResourceLoadSettings"/>. /// </summary> /// <param name="source">A <see cref="Uri"/> that points to the location of the web resource used to load the syndication resource.</param> /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param> /// <param name="settings">The <see cref="SyndicationResourceLoadSettings"/> object used to configure the <see cref="AtomEntry"/> instance. This value can be <b>null</b>.</param> /// <remarks> /// <para> /// <list type="bullet"> /// <item> /// <description> /// If <paramref name="settings"/> has a <see cref="SyndicationResourceLoadSettings.CharacterEncoding">character encoding</see> of <see cref="System.Text.Encoding.UTF8"/> /// the character encoding of the <paramref name="source"/> will be attempt to be determined automatically, otherwise the specified character encoding will be used. /// If automatic detection fails, a character encoding of <see cref="System.Text.Encoding.UTF8"/> is used by default. /// </description> /// </item> /// <item> /// <description> /// After the load operation has successfully completed, the <see cref="AtomEntry.Loaded"/> event will be raised. /// </description> /// </item> /// </list> /// </para> /// </remarks> /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="FormatException">The <paramref name="source"/> data does not conform to the expected syndication content format. In this case, the entry remains empty.</exception> /// <exception cref="XmlException">There is a load or parse error in the XML. In this case, the entry remains empty.</exception> public new void Load(Uri source, WebRequestOptions options, SyndicationResourceLoadSettings settings) { base.Load(source, options, settings); AtomPublishingEditedSyndicationExtension editedExtension = this.FindExtension(AtomPublishingEditedSyndicationExtension.MatchByType) as AtomPublishingEditedSyndicationExtension; if (editedExtension != null) { this.EditedOn = editedExtension.Context.EditedOn; } AtomPublishingControlSyndicationExtension controlExtension = this.FindExtension(AtomPublishingControlSyndicationExtension.MatchByType) as AtomPublishingControlSyndicationExtension; if (controlExtension != null) { this.IsDraft = controlExtension.Context.IsDraft; } }
/// <summary> /// Saves the syndication resource to the specified <see cref="XmlWriter"/> and <see cref="SyndicationResourceSaveSettings"/>. /// </summary> /// <param name="writer">The <b>XmlWriter</b> to which you want to save the syndication resource.</param> /// <param name="settings">The <see cref="SyndicationResourceSaveSettings"/> object used to configure the persistance of the <see cref="AtomEntry"/> instance.</param> /// <exception cref="ArgumentNullException">The <paramref name="writer"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException">The <paramref name="settings"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="XmlException">The operation would not result in well formed XML for the syndication resource.</exception> public new void Save(XmlWriter writer, SyndicationResourceSaveSettings settings) { //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ Guard.ArgumentNotNull(writer, "writer"); Guard.ArgumentNotNull(settings, "settings"); //------------------------------------------------------------ // Modify syndication extensions based on properties //------------------------------------------------------------ List <ISyndicationExtension> list = new List <ISyndicationExtension>(this.Extensions); if (this.EditedOn != DateTime.MinValue) { if (!list.Exists(AtomPublishingEditedSyndicationExtension.MatchByType)) { AtomPublishingEditedSyndicationExtension editedExtension = new AtomPublishingEditedSyndicationExtension(); editedExtension.Context.EditedOn = this.EditedOn; this.AddExtension(editedExtension); } } if (this.IsDraft) { if (!list.Exists(AtomPublishingControlSyndicationExtension.MatchByType)) { AtomPublishingControlSyndicationExtension controlExtension = new AtomPublishingControlSyndicationExtension(); controlExtension.Context.IsDraft = this.IsDraft; this.AddExtension(controlExtension); } } //------------------------------------------------------------ // Save member resource //------------------------------------------------------------ base.Save(writer, settings); }
/// <summary> /// Loads the syndication resource from the supplied <see cref="Uri"/> using the specified <see cref="ICredentials">credentials</see>, <see cref="IWebProxy">proxy</see> and <see cref="SyndicationResourceLoadSettings"/>. /// </summary> /// <param name="source">A <see cref="Uri"/> that points to the location of the web resource used to load the syndication resource.</param> /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param> /// <param name="settings">The <see cref="SyndicationResourceLoadSettings"/> object used to configure the <see cref="AtomEntry"/> instance. This value can be <b>null</b>.</param> /// <remarks> /// <para> /// <list type="bullet"> /// <item> /// <description> /// If <paramref name="settings"/> has a <see cref="SyndicationResourceLoadSettings.CharacterEncoding">character encoding</see> of <see cref="System.Text.Encoding.UTF8"/> /// the character encoding of the <paramref name="source"/> will be attempt to be determined automatically, otherwise the specified character encoding will be used. /// If automatic detection fails, a character encoding of <see cref="System.Text.Encoding.UTF8"/> is used by default. /// </description> /// </item> /// <item> /// <description> /// After the load operation has successfully completed, the <see cref="AtomEntry.Loaded"/> event will be raised. /// </description> /// </item> /// </list> /// </para> /// </remarks> /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="FormatException">The <paramref name="source"/> data does not conform to the expected syndication content format. In this case, the entry remains empty.</exception> /// <exception cref="XmlException">There is a load or parse error in the XML. In this case, the entry remains empty.</exception> public new void Load(Uri source, WebRequestOptions options, SyndicationResourceLoadSettings settings) { //------------------------------------------------------------ // Load syndication resource using base entry implementation //------------------------------------------------------------ base.Load(source, options, settings); //------------------------------------------------------------ // Extract member resource information from extensions //------------------------------------------------------------ AtomPublishingEditedSyndicationExtension editedExtension = this.FindExtension(AtomPublishingEditedSyndicationExtension.MatchByType) as AtomPublishingEditedSyndicationExtension; if (editedExtension != null) { this.EditedOn = editedExtension.Context.EditedOn; } AtomPublishingControlSyndicationExtension controlExtension = this.FindExtension(AtomPublishingControlSyndicationExtension.MatchByType) as AtomPublishingControlSyndicationExtension; if (controlExtension != null) { this.IsDraft = controlExtension.Context.IsDraft; } }
//============================================================ // CALLBACK DELEGATE METHODS //============================================================ #region AsyncLoadCallback(IAsyncResult result) /// <summary> /// Called when a corresponding asynchronous load operation completes. /// </summary> /// <param name="result">The result of the asynchronous operation.</param> private static void AsyncLoadCallback(IAsyncResult result) { //------------------------------------------------------------ // Local members //------------------------------------------------------------ System.Text.Encoding encoding = System.Text.Encoding.UTF8; XPathNavigator navigator = null; WebRequest httpWebRequest = null; AtomEntryResource entry = null; Uri source = null; WebRequestOptions options = null; SyndicationResourceLoadSettings settings = null; //------------------------------------------------------------ // Determine if the async send operation completed //------------------------------------------------------------ if (result.IsCompleted) { //------------------------------------------------------------ // Extract the send operations parameters from the user state //------------------------------------------------------------ object[] parameters = (object[])result.AsyncState; httpWebRequest = parameters[0] as WebRequest; entry = parameters[1] as AtomEntryResource; source = parameters[2] as Uri; settings = parameters[3] as SyndicationResourceLoadSettings; options = parameters[4] as WebRequestOptions; object userToken = parameters[5]; //------------------------------------------------------------ // Verify expected parameters were found //------------------------------------------------------------ if (entry != null) { //------------------------------------------------------------ // Get the response to the syndication resource request //------------------------------------------------------------ WebResponse httpWebResponse = (WebResponse)httpWebRequest.EndGetResponse(result); //------------------------------------------------------------ // Load syndication resource //------------------------------------------------------------ using (Stream stream = httpWebResponse.GetResponseStream()) { if (settings != null) { encoding = settings.CharacterEncoding; } using (StreamReader streamReader = new StreamReader(stream, encoding)) { XmlReaderSettings readerSettings = new XmlReaderSettings(); readerSettings.IgnoreComments = true; readerSettings.IgnoreWhitespace = true; readerSettings.ProhibitDtd = false; using (XmlReader reader = XmlReader.Create(streamReader, readerSettings)) { if (encoding == System.Text.Encoding.UTF8) { navigator = SyndicationEncodingUtility.CreateSafeNavigator(source, options, null); } else { navigator = SyndicationEncodingUtility.CreateSafeNavigator(source, options, settings.CharacterEncoding); } //------------------------------------------------------------ // Load syndication resource using the framework adapters //------------------------------------------------------------ SyndicationResourceAdapter adapter = new SyndicationResourceAdapter(navigator, settings); adapter.Fill(entry, SyndicationContentFormat.Atom); //------------------------------------------------------------ // Extract member resource information from extensions //------------------------------------------------------------ AtomPublishingEditedSyndicationExtension editedExtension = entry.FindExtension(AtomPublishingEditedSyndicationExtension.MatchByType) as AtomPublishingEditedSyndicationExtension; if (editedExtension != null) { entry.EditedOn = editedExtension.Context.EditedOn; } AtomPublishingControlSyndicationExtension controlExtension = entry.FindExtension(AtomPublishingControlSyndicationExtension.MatchByType) as AtomPublishingControlSyndicationExtension; if (controlExtension != null) { entry.IsDraft = controlExtension.Context.IsDraft; } //------------------------------------------------------------ // Raise Loaded event to notify registered handlers of state change //------------------------------------------------------------ entry.OnEntryLoaded(new SyndicationResourceLoadedEventArgs(navigator, source, options, userToken)); } } } //------------------------------------------------------------ // Reset load operation in progress indicator //------------------------------------------------------------ entry.LoadOperationInProgress = false; } } }
/// <summary> /// Called when a corresponding asynchronous load operation completes. /// </summary> /// <param name="result">The result of the asynchronous operation.</param> private static void AsyncLoadCallback(IAsyncResult result) { System.Text.Encoding encoding = System.Text.Encoding.UTF8; XPathNavigator navigator = null; WebRequest httpWebRequest = null; AtomEntryResource entry = null; Uri source = null; WebRequestOptions options = null; SyndicationResourceLoadSettings settings = null; if (result.IsCompleted) { object[] parameters = (object[])result.AsyncState; httpWebRequest = parameters[0] as WebRequest; entry = parameters[1] as AtomEntryResource; source = parameters[2] as Uri; settings = parameters[3] as SyndicationResourceLoadSettings; options = parameters[4] as WebRequestOptions; object userToken = parameters[5]; if (entry != null) { WebResponse httpWebResponse = (WebResponse)httpWebRequest.EndGetResponse(result); using (Stream stream = httpWebResponse.GetResponseStream()) { if (settings != null) { encoding = settings.CharacterEncoding; } using (StreamReader streamReader = new StreamReader(stream, encoding)) { XmlReaderSettings readerSettings = new XmlReaderSettings(); readerSettings.IgnoreComments = true; readerSettings.IgnoreWhitespace = true; readerSettings.DtdProcessing = DtdProcessing.Ignore; using (XmlReader reader = XmlReader.Create(streamReader, readerSettings)) { if (encoding == System.Text.Encoding.UTF8) { navigator = SyndicationEncodingUtility.CreateSafeNavigator(source, options, null); } else { navigator = SyndicationEncodingUtility.CreateSafeNavigator(source, options, settings.CharacterEncoding); } SyndicationResourceAdapter adapter = new SyndicationResourceAdapter(navigator, settings); adapter.Fill(entry, SyndicationContentFormat.Atom); AtomPublishingEditedSyndicationExtension editedExtension = entry.FindExtension(AtomPublishingEditedSyndicationExtension.MatchByType) as AtomPublishingEditedSyndicationExtension; if (editedExtension != null) { entry.EditedOn = editedExtension.Context.EditedOn; } AtomPublishingControlSyndicationExtension controlExtension = entry.FindExtension(AtomPublishingControlSyndicationExtension.MatchByType) as AtomPublishingControlSyndicationExtension; if (controlExtension != null) { entry.IsDraft = controlExtension.Context.IsDraft; } entry.OnEntryLoaded(new SyndicationResourceLoadedEventArgs(navigator, source, options, userToken)); } } } entry.LoadOperationInProgress = false; } } }