Пример #1
0
        public async Task <string> GetOpmlDataAsync(OpmlDoc opmlDoc)
        {
            var sb = new StringBuilder();

            var writerSettings = new XmlWriterSettings {
                Encoding = Encoding.UTF8, Async = true
            };

            await using (var writer = XmlWriter.Create(sb, writerSettings))
            {
                // open OPML
                writer.WriteStartElement("opml");

                await writer.WriteAttributeStringAsync("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema");

                await writer.WriteAttributeStringAsync("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");

                writer.WriteAttributeString("version", "1.0");

                // open HEAD
                writer.WriteStartElement("head");
                writer.WriteStartElement("title");
                writer.WriteValue(opmlDoc.SiteTitle);
                await writer.WriteEndElementAsync();

                await writer.WriteEndElementAsync();

                // open BODY
                writer.WriteStartElement("body");

                // allrss
                writer.WriteStartElement("outline");
                writer.WriteAttributeString("title", "All Posts");
                writer.WriteAttributeString("text", "All Posts");
                writer.WriteAttributeString("type", "rss");
                writer.WriteAttributeString("xmlUrl", opmlDoc.XmlUrl);
                writer.WriteAttributeString("htmlUrl", opmlDoc.HtmlUrl);
                await writer.WriteEndElementAsync();

                // categories
                foreach (var cat in opmlDoc.ContentInfo)
                {
                    // open OUTLINE
                    writer.WriteStartElement("outline");

                    writer.WriteAttributeString("title", cat.Key);
                    writer.WriteAttributeString("text", cat.Value);
                    writer.WriteAttributeString("type", "rss");
                    writer.WriteAttributeString("xmlUrl", opmlDoc.XmlUrlTemplate.Replace("[catTitle]", cat.Value).ToLower());
                    writer.WriteAttributeString("htmlUrl", opmlDoc.HtmlUrlTemplate.Replace("[catTitle]", cat.Value).ToLower());

                    // close OUTLINE
                    await writer.WriteEndElementAsync();
                }

                // close BODY
                await writer.WriteEndElementAsync();

                // close OPML
                await writer.WriteEndElementAsync();
            }

            var xml = sb.ToString();

            return(xml);
        }
Пример #2
0
        public async Task WriteOpmlFileAsync(string opmlFilePath, OpmlDoc opmlDoc)
        {
            await using var fs = new FileStream(opmlFilePath, FileMode.Create, FileAccess.Write, FileShare.None, 4096, true);
            var writerSettings = new XmlWriterSettings {
                Encoding = Encoding.UTF8, Indent = true, Async = true
            };

            using (var writer = XmlWriter.Create(fs, writerSettings))
            {
                // open OPML
                writer.WriteStartElement("opml");

                await writer.WriteAttributeStringAsync("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema");

                await writer.WriteAttributeStringAsync("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");

                writer.WriteAttributeString("version", "1.0");

                // open HEAD
                writer.WriteStartElement("head");
                writer.WriteStartElement("title");
                writer.WriteValue(opmlDoc.SiteTitle);
                await writer.WriteEndElementAsync();

                await writer.WriteEndElementAsync();

                // open BODY
                writer.WriteStartElement("body");

                // allrss
                writer.WriteStartElement("outline");
                writer.WriteAttributeString("title", "All Posts");
                writer.WriteAttributeString("text", "All Posts");
                writer.WriteAttributeString("type", "rss");
                writer.WriteAttributeString("xmlUrl", opmlDoc.XmlUrl);
                writer.WriteAttributeString("htmlUrl", opmlDoc.HtmlUrl);
                await writer.WriteEndElementAsync();

                // categories
                foreach (var cat in opmlDoc.CategoryInfo)
                {
                    // open OUTLINE
                    writer.WriteStartElement("outline");

                    writer.WriteAttributeString("title", cat.Key);
                    writer.WriteAttributeString("text", cat.Value);
                    writer.WriteAttributeString("type", "rss");
                    writer.WriteAttributeString("xmlUrl", opmlDoc.CategoryXmlUrlTemplate.Replace("[catTitle]", cat.Value).ToLower());
                    writer.WriteAttributeString("htmlUrl", opmlDoc.CategoryHtmlUrlTemplate.Replace("[catTitle]", cat.Value).ToLower());

                    // close OUTLINE
                    await writer.WriteEndElementAsync();
                }

                // close BODY
                await writer.WriteEndElementAsync();

                // close OPML
                await writer.WriteEndElementAsync();
            }
            await fs.FlushAsync();
        }