Obtaining Indented XML as a String

Using XmlDocument.Save(string file) produces a file with nicely indented elements. XmlDocument.OuterXml returns a string without any formatting. If you want a nicely formatted string (to display to the user, write to console, etc), without directly writing to a file, you must use the XmlTextWriter. The using statements in the method makes sure the resorces are disposed of sooner than later. Here is the code to return a string of formatted XML from an XmlDocument. Download Example Code (requires Visual Studio 2005) using System.IO; using System.Xml; public static string FormatXML(XmlDocument doc) { // Create a stream buffer that can be read … Continue reading Obtaining Indented XML as a String

Well Formatted Blog, Can’t Directly Copy from MS Word

Wow!  Well I just had to go back through all my posts and repost them all.  It seams that one can not directly copy and paste from a Microsoft Word document to achieve a nicely formatted blog.   Invalid XML code is the end result causing the RSS feed to break.  This not only affects my blog’s feed, but also the main MSMVPS.org feed.   Thanks to Miha Markic for pointing this out quickly before I had posted much more than the first 10 initial posts!! http://www.rthand.com/   I’ve spent much time checking and rechecking the RSS feed and it … Continue reading Well Formatted Blog, Can’t Directly Copy from MS Word

Assimilate XML RSS Feed from URL in C#

This piece of code pulls an RSS feed and displays the title of the articles.  It is here to demonstrate just how easy it is to pull XML off the web and utilize the data.   I use Trace.WriteLine instead of Console.WriteLine so the report is fed to the Visual Studio output window and can be run as either a Console or Windows app, but at least a new Console window is not opened.     using System.Xml; using System.Net; using System.Text; using System.Diagnostics;   // ——————————-   // RSS XML Feed URL string url = “http://msmvps.com/coad/rss.aspx”;   // Create … Continue reading Assimilate XML RSS Feed from URL in C#

JScript XML Data Access

“Random Vocabulary Word.js”   This simple JScript demonstrates just how easy it is to access data from an XML file through the Windows Scripting Host and MSHTML 4.0.  You must have “Vocabulary.xml” in the same directory when running.     // Created by Noah Coad, coad.net/noah, 4/13/04   xmlFile = “Vocabulary.xml”;   var xml = new ActiveXObject(“MSXML2.DOMDocument.4.0”); xml.validateOnParse = false; xml.async = false; xml.load(xmlFile);   if (xml.parseError.errorCode != 0)    WScript.Echo(“XML Parse Error : ” + xml.parseError.reason); else {    var root = xml.documentElement;    var oNodeList = root.childNodes;      // Select a random word    var rnd = … Continue reading JScript XML Data Access

Simple C# XSL Transformation

“XSLTransform.cs”                                      And here is a simple C# translator that takes an XML file sends it through an XSL file transform and saves the output.  It is intended to be used as a console application.  This can be easily used to apply the CSV transform XSL to Vocabulary.xml.     // Created by Noah Coad, coad.net/noah, noah@coad.net, 4/10/04   using System; using System.IO; using System.Xml; using System.Xml.Xsl; using System.Diagnostics;   namespace XSLTransform {    class Transform    {       private Transform(string[] args)       {          // Make sure at least three command          // line arguments are specified          … Continue reading Simple C# XSL Transformation

XSL Transform XML to CSV

“VocabularyToCSV.xsl”   Since Comma Separated Value (CSV) files are so common, here is a transformation.  It requires “Vocabulary.xml” and a translator.     <?xml version=”1.0″?>   <!— Created by Noah Coad, coad.net/noah, noah@coad.net, 3/23/04 —>   <xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>    <xsl:output method=”text” encoding=”ISO-8859-1″/>    <xsl:strip-space elements=”Set”/>    <xsl:template match=”/Vocabulary”>Word,Description <xsl:apply-templates select=”Set”>          <xsl:sort select=”Word”/>       </xsl:apply-templates>    </xsl:template>      <xsl:template match=”Set”>       <xsl:value-of select=”Word”/>,”<xsl:value-of select=”Desc”/>“<xsl:text> </xsl:text>    </xsl:template> </xsl:stylesheet>    

HTML With XML Data Binding

“Vocabulary.html”   This is used in conjunction with “Vocabulary.xml” to demonstrate IE HTML data binding.  Without any scripting, this will pull data from the XML file and display it in the HTML page.  This is considerably different than the XSLT that converts XML into HTML.  Note that this is also a well-formatted XHTML document.     <?xml version=”1.0″?> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>   <!— Created by Noah Coad, coad.net/noah, noah@coad.net, 3/23/04 —> <!— This is a proper XHTML document. —>   <html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>    <head>       <title>Vocabulary Words</title>       <style type=”text/css”>          body            {font: … Continue reading HTML With XML Data Binding

XSL Translation from XML to HTML

“Vocabulary.xsl”   This is used in conjunction with “Vocabulary.xml” to demonstrate the power of XSL translation.  Put both in the same directory, load the .xml file in IE, and IE will automatically apply the transformation into HTML.   Key points: Translation from XML to HTML, Sorting on the Word elements, and Matching within the Ancor tag     <?xml version=”1.0″?>   <!— Created by Noah Coad, coad.net/noah, noah@coad.net, 3/23/04 —>   <xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>    <xsl:template match=”/Vocabulary”>      <html>      <head>      <title>Vocabulary Words</title>      <style type=”text/css”>          body        {font: 10pt verdana;}      … Continue reading XSL Translation from XML to HTML

Sample XML Data, Vocabulary.xml

“Vocabulary.xml”   This is my first code posting so bear with as they get better.  This is a sample XML file that will be used for a number of the other postings.     <?xml version=”1.0″ standalone=”yes”?> <?xml-stylesheet type=“text/xsl” href=“Vocabulary.xsl”?> <Vocabulary>   <Set>     <Word>Prerogative</Word>     <Desc>An exclusive right or privilege held by a person or group, especially a hereditary or official right.</Desc>   </Set>   <Set>     <Word>Inextricably</Word>     <Desc>Unavoidable; inescapable.  So intricate or entangled as to make escape impossible.  Too involved or complicated to solve.</Desc>   </Set>   <Set>     <Word>Eclectic</Word>     <Desc>Selecting or employing individual elements from … Continue reading Sample XML Data, Vocabulary.xml