Use C# to generate XML (RSS) in two ways

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Use C# to generate XML (RSS) in two ways:

A, use XmlWriter to create the XML file:

  1 using System;
  2 
  3 using System.Collections.Generic;
  4 
  5 using System.Text;
  6 
  7 using System.IO;
  8 
  9 using System.Xml;
 10 
 11  
 12 
 13 namespace UseXmlWriter
 14 
 15 {
 16 
 17     class Program
 18 
 19     {
 20 
 21         static void Main(string[] args)
 22 
 23         {
 24 
 25             using (MemoryStream ms = new MemoryStream())
 26 
 27             {
 28 
 29                 XmlWriterSettings settings = new XmlWriterSettings();
 30 
 31                 //Demanding indent
 32 
 33                 settings.Indent = true;
 34 
 35                 //Note that if you do not set the default encoding will output utf-16
 36 
 37                 //Note here can not directly use Encoding.UTF8 if non XML content added 4 bytes in the output text will use Encoding.UTF8
 38 
 39                 settings.Encoding = new UTF8Encoding(false);
 40 
 41                  
 42 
 43                 //Setting a newline
 44 
 45                 settings.NewLineChars = Environment.NewLine;
 46 
 47  
 48 
 49                 using (XmlWriter xmlWriter = XmlWriter.Create(ms, settings))
 50 
 51                 {
 52 
 53  
 54 
 55                     //Write XML file<?xml version="1.0" encoding="utf-8" ?>
 56 
 57                     xmlWriter.WriteStartDocument(false);
 58 
 59                     //Write the root node
 60 
 61                     xmlWriter.WriteStartElement("root");
 62 
 63                     //Office node
 64 
 65                     xmlWriter.WriteStartElement("cat");
 66 
 67                     //Add the attribute to the node
 68 
 69                     xmlWriter.WriteAttributeString("color", "white");
 70 
 71                     //To add text to the internal nodes
 72 
 73                     xmlWriter.WriteString("I'm a cat");
 74 
 75                     xmlWriter.WriteEndElement();
 76 
 77  
 78 
 79  
 80 
 81                     //Through the WriteElementString can add a node at the same time, add the node content
 82 
 83                     xmlWriter.WriteElementString("pig", "pig is great");
 84 
 85  
 86 
 87  
 88 
 89                     xmlWriter.WriteStartElement("dog");
 90 
 91                     //Write CData
 92 
 93                     xmlWriter.WriteCData("<strong>dog is dog</strong>");
 94 
 95                     xmlWriter.WriteEndElement();
 96 
 97      
 98                     //Add comments
 99                     xmlWriter.WriteComment("this is an example writed");
100 
101  
102 
103                     xmlWriter.WriteEndElement();
104 
105                     xmlWriter.WriteEndDocument();
106 
107  
108 
109                 }
110 
111  
112 
113                 //XML content will be output to the console
114 
115                 string xml = Encoding.UTF8.GetString(ms.ToArray());
116 
117                 Console.WriteLine(xml);
118 
119             }
120 
121             Console.Read();
122 
123  
124 
125         }
126 
127     }
128 
129 }        

Two, use Linq To XML to create a RSS source file:

  1 using System;
  2 
  3 
  4 using System.Web;
  5 
  6 
  7 using System.Xml;
  8 
  9 
 10 using System.Xml.Linq;
 11 
 12 
 13 
 14 
 15 namespace WebApplication1
 16 
 17 
 18 {
 19 
 20 
 21     public class RssHttpHandler : IHttpHandler
 22 
 23 
 24     {
 25 
 26 
 27 
 28 
 29         public void ProcessRequest(HttpContext context)
 30 
 31 
 32         {
 33 
 34 
 35             context.Response.ContentType = "text/xml";
 36 
 37 
 38 
 39 
 40             context.Response.Write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
 41 
 42 
 43 
 44 
 45             XElement rssFeed = new XElement("rss", new XAttribute("version", "2.0"));
 46 
 47 
 48 
 49 
 50             string fixedUrl = "http://www.freeflying.com/article";
 51 
 52 
 53             string wholeUrl = string.Empty;
 54 
 55 
 56 
 57 
 58             XElement channel = new XElement("channel",
 59 
 60 
 61                 new XElement("title", "freeflying"),
 62 
 63 
 64                 new XElement("link", fixedUrl),
 65 
 66 
 67                 new XElement("description", "the website for dream flying freely"),
 68 
 69 
 70                 new XElement("pubDate", DateTime.Now.ToString())
 71 
 72 
 73                 );
 74 
 75 
 76 
 77 
 78 
 79 
 80             foreach (var article in Articles.GetArticles())
 81 
 82 
 83             {
 84 
 85 
 86                 XElement item = new XElement("item");
 87 
 88 
 89 
 90 
 91                 XElement title = new XElement("title", article.Title);
 92 
 93 
 94 
 95 
 96                 wholeUrl = string.Format("{0}?id={1}&catelog={2}", fixedUrl, article.ID, article.Catelog);
 97 
 98 
 99                 XElement link = new XElement("link", wholeUrl);
100 
101 
102 
103 
104                 XElement description = new XElement("description", article.Description);
105 
106 
107 
108 
109                 XElement pubDate = new XElement("pubDate", article.LastMod.ToString());
110 
111 
112 
113 
114                 item.Add(title, link, description, pubDate);
115 
116 
117 
118 
119                 channel.Add(item);
120 
121 
122             }
123 
124 
125 
126 
127             rssFeed.Add(channel);
128 
129 
130 
131 
132             context.Response.Write(rssFeed);
133 
134 
135 
136 
137         }
138 
139 
140 
141 
142         public bool IsReusable
143 
144 
145         {
146 
147 
148             get { return false; }
149 
150 
151         }
152 
153 
154     }
155 
156 
157 }

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Warren at November 13, 2013 - 12:41 PM