Hey guys!! This post show you how to use XPath in C#.
I wrote program for formatting rss feed (xml) to html for its example.
For Html rendering.
For XmlDocument creation.
This is simple abstract template class for helping html rendering, nothing special.
I wrote program for formatting rss feed (xml) to html for its example.
XPath in C#
The simplest way is just creating XmlDocument and call SelectNodes method.var xmlString = "some xml string...." // create XmlDocment var doc = new XmlDocument(); doc.LoadXml(xmlString); // selecting nodes by xpath string doc.SelectNodes("/rss/channel/item");
RSS Xml to Html Example
Okay here is a simple XPath example - converting rss xml to html.using System.IO; using System.Web.UI; using System.Web; namespace Utility { public class RSStoHtmlWriter : HTMLWriteHelper { private readonly string url; public RSStoHtmlWriter(string url) { this.url = url; } public override void WriteBody(HtmlTextWriter htmlWriter) { using (var reader = new XmlTextReader(url)) { // **** using XPath!! **** // As you notice, ToXmlDocument is not implemented in XmlTextReader. // I wrote it as an extension method. Pls see bottom of this post. foreach (var item in reader.ToXmlDocument().SelectNodes("/rss/channel/item")) { var node = item as XmlNode; if (node != null) { WriteItem(htmlWriter, node); } } } } // render xml node as html protected void WriteItem(HtmlTextWriter htmlWriter, XmlNode node) { string title = HttpUtility.HtmlDecode(node.SelectSingleNode("./title").FirstChild.Value); string pubDate = HttpUtility.HtmlDecode(node.SelectSingleNode("./pubDate").FirstChild.Value); htmlWriter.RenderBeginTag(HtmlTextWriterTag.H2); htmlWriter.Write(title); htmlWriter.RenderEndTag(); htmlWriter.Write(FormatPubDate(pubDate)); htmlWriter.WriteBreak(); htmlWriter.WriteBreak(); htmlWriter.RenderBeginEndTag(HtmlTextWriterTag.Hr); // extension method again } protected static string FormatPubDate(string pubDate) { int index = pubDate.IndexOf(" +"); if (index > 0) { pubDate = pubDate.Remove(index); } return string.Format("{0:MM/dd/yyyy hh:mm tt}", DateTime.Parse(pubDate)); } } }
Trivial Utility Methods
The following classes and methods are just for trivial helper stuffs used above example.For Html rendering.
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO; using System.Web.UI; namespace Utility { public static class HttpUtils { // extension method for rendering empty html tag quickly public static void RenderBeginEndTag(this HtmlTextWriter htmlWriter, HtmlTextWriterTag tag) { htmlWriter.RenderBeginTag(tag); htmlWriter.RenderEndTag(); } } }
For XmlDocument creation.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.IO; namespace Utility { public static class XMLUtils { // trivial extension method for creating XmlDocumen from XmlReader public static XmlDocument ToXmlDocument(this XmlReader reader) { var doc = new XmlDocument(); doc.Load(reader); return doc; } } }
This is simple abstract template class for helping html rendering, nothing special.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.UI; using System.IO; namespace Utility { public abstract class HTMLWriteHelper { public virtual void WriteBody(HtmlTextWriter htmlWriter){} public virtual void WriteHeader(HtmlTextWriter htmlWriter){} public void WriteToFile(string filepath) { using (var streamWriter = new StreamWriter(filepath, false)) { WriteTo(streamWriter); } } public void WriteTo(TextWriter textWriter) { using (var htmlWriter = new HtmlTextWriter(textWriter)) { htmlWriter.RenderBeginTag(HtmlTextWriterTag.Html); htmlWriter.RenderBeginTag(HtmlTextWriterTag.Head); WriteHeader(htmlWriter); htmlWriter.RenderEndTag(); htmlWriter.RenderBeginTag(HtmlTextWriterTag.Body); WriteBody(htmlWriter); htmlWriter.RenderEndTag(); htmlWriter.RenderEndTag(); htmlWriter.Close(); } } } }
コメント
Ellipse Mouse Down event