From 4222717a5fe0ca6aeb2359270d48e9d6a674eac6 Mon Sep 17 00:00:00 2001 From: Noah Wood Date: Mon, 20 Apr 2020 17:38:10 -0400 Subject: [PATCH 1/2] Added ability to have NULL Uris. I write an application that can sometimes encounter NULL URI elements and want to handle them myself after the RSS feed is parsed rather than fail. --- src/Rss/RssParser.cs | 25 ++++++++++++++++--------- src/SyndicationLink.cs | 2 +- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/Rss/RssParser.cs b/src/Rss/RssParser.cs index 4f9b0b7..74878ef 100644 --- a/src/Rss/RssParser.cs +++ b/src/Rss/RssParser.cs @@ -11,6 +11,11 @@ namespace Microsoft.SyndicationFeed.Rss { public class RssParser : ISyndicationFeedParser { + public bool AllowNullLinks { get; set; } + + public RssParser() { } + public RssParser(bool AllowNullLinks) => this.AllowNullLinks = AllowNullLinks; + public ISyndicationCategory ParseCategory(string value) { ISyndicationContent content = ParseContent(value); @@ -192,6 +197,10 @@ public virtual ISyndicationLink CreateLink(ISyndicationContent content) throw new ArgumentNullException(nameof(content)); } + // + // Reserve for possible empty url + bool isNullUri = false; + // // Title string title = content.Value; @@ -203,21 +212,19 @@ public virtual ISyndicationLink CreateLink(ISyndicationContent content) if (url != null) { - if (!TryParseValue(url, out uri)) - { - throw new FormatException("Invalid url attribute"); - } + isNullUri = !TryParseValue(url, out uri); } else { - if (!TryParseValue(content.Value, out uri)) - { - throw new FormatException("Invalid url"); - } - + isNullUri = !TryParseValue(content.Value, out uri); title = null; } + if (!AllowNullLinks && isNullUri) + { + throw new FormatException("Invalid url"); + } + // // Length long length = 0; diff --git a/src/SyndicationLink.cs b/src/SyndicationLink.cs index 819dfde..7d90547 100644 --- a/src/SyndicationLink.cs +++ b/src/SyndicationLink.cs @@ -10,7 +10,7 @@ public sealed class SyndicationLink : ISyndicationLink { public SyndicationLink(Uri url, string relationshipType = null) { - Uri = url ?? throw new ArgumentNullException(nameof(url)); + Uri = url; RelationshipType = relationshipType; } From 6fc437ebbfd4bc50e35d8e90d58f2f538fc249d8 Mon Sep 17 00:00:00 2001 From: Noah Wood Date: Mon, 3 Jan 2022 10:27:17 -0500 Subject: [PATCH 2/2] Adds missing namespaces in the examples for encoding. --- examples/CreateSimpleRssFeedExample.cs | 1 + examples/RssWriteItemWithCustomElementExample.cs | 1 + 2 files changed, 2 insertions(+) diff --git a/examples/CreateSimpleRssFeedExample.cs b/examples/CreateSimpleRssFeedExample.cs index 3e5e429..d5695de 100644 --- a/examples/CreateSimpleRssFeedExample.cs +++ b/examples/CreateSimpleRssFeedExample.cs @@ -5,6 +5,7 @@ using Microsoft.SyndicationFeed; using Microsoft.SyndicationFeed.Rss; using System; +using System.Text; using System.IO; using System.Threading.Tasks; using System.Xml; diff --git a/examples/RssWriteItemWithCustomElementExample.cs b/examples/RssWriteItemWithCustomElementExample.cs index 414b6c5..29ce4d9 100644 --- a/examples/RssWriteItemWithCustomElementExample.cs +++ b/examples/RssWriteItemWithCustomElementExample.cs @@ -5,6 +5,7 @@ using Microsoft.SyndicationFeed; using Microsoft.SyndicationFeed.Rss; using System; +using System.Text; using System.Collections.Generic; using System.IO; using System.Threading.Tasks;