This repository was archived by the owner on Dec 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathRssWriteItemWithCustomElementExample.cs
More file actions
76 lines (61 loc) · 2.42 KB
/
RssWriteItemWithCustomElementExample.cs
File metadata and controls
76 lines (61 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.SyndicationFeed;
using Microsoft.SyndicationFeed.Rss;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.Xml;
/// <summary>
/// Create a SyndicationItem and add a custom field.
/// </summary>
class RssWriteItemWithCustomElement
{
public static async Task WriteCustomItem()
{
const string ExampleNs = "http://contoso.com/syndication/feed/examples";
var sw = new StringWriterWithEncoding(Encoding.UTF8);
using (XmlWriter xmlWriter = XmlWriter.Create(sw, new XmlWriterSettings() { Async = true, Indent = true }))
{
var attributes = new List<SyndicationAttribute>()
{
new SyndicationAttribute("xmlns:example", ExampleNs)
};
var formatter = new RssFormatter(attributes, xmlWriter.Settings);
var writer = new RssFeedWriter(xmlWriter, attributes, formatter);
// Create item
var item = new SyndicationItem()
{
Title = "Rss Writer Available",
Description = "The new RSS Writer is now available as a NuGet package!",
Id = "https://www.nuget.org/packages/Microsoft.SyndicationFeed",
Published = DateTimeOffset.UtcNow
};
item.AddCategory(new SyndicationCategory("Technology"));
item.AddContributor(new SyndicationPerson("test", "test@mail.com"));
//
// Format the item as SyndicationContent
var content = new SyndicationContent(formatter.CreateContent(item));
// Add custom fields/attributes
content.AddField(new SyndicationContent("customElement", ExampleNs, "Custom Value"));
// Write
await writer.Write(content);
// Done
xmlWriter.Flush();
}
Console.WriteLine(sw.ToString());
}
class StringWriterWithEncoding : StringWriter
{
private readonly Encoding _encoding;
public StringWriterWithEncoding(Encoding encoding)
{
this._encoding = encoding;
}
public override Encoding Encoding {
get { return _encoding; }
}
}
}