The World’s Leading Microsoft .NET Magazine
   
 
timstall

Donate Today!

Search Box

 

Calendar

««Feb 2010»»
SMTWTFS
 
1
23456
78910111213
14151617181920
21222324252627
28

My RSS Feeds








Mailing List

Most Popular Tags

                                                           

Having Console apps write out Xml so you can parse their output

posted Thursday, 9 October 2008
Say you have an automatic process calling some executable, and you want to get information back from that executable to use in your own code. Ideally you'd be able to call a class library that you could just reference, but what if no such class library is available (it's a different development platform like Java vs. C#, or it's a black box third-party component). Is there any way to salvage the situation?

 

Some developers would stream out the console output to a string, and then write a bunch of ugly parse code (even with regular expressions), and get their results that way. For example, let's say you use Subversion for your source control, and you want to search the history log. For the sake of argument, assume there's no friendly C# API or web service call (it's a local desktop app you're calling), and you need to parse command output. (If anyone knows of such an API, please feel free to suggest it in the comments.) You can run a command like "svn log C:\Projects\MyProject", and it will give you back console output like so:

------------------------------------------------------------------------
r3004 | username1 | 2008-09-26 10:47:19 -0500 (Fri, 26 Sep 2008) | 2 lines

Solved world hunger
------------------------------------------------------------------------
r3000 | username1 | 2008-09-06 14:10:56 -0500 (Sat, 06 Sep 2008) | 2 lines

Invented nuclear fusion
 

Ok, you can parse all the information there, but it's very error prone. A much better way is if the console app provides an option to write out its output as a single XML string. For example, you can specify the "--xml" parameter in SVN to do just that:

<log>
  <logentry
     revision="3004">
    <author>username1 </author>
    <date>2008-09-26 10:47:19 -0500</date>
    <msg>
      Solved world hunger
    </msg>
  </logentry>
  <logentry
     revision="3000 ">
    <author>username1 </author>
    <date>2008-09-06 14:10:56 -0500</date>
    <msg>
      Invented nuclear fusion
    </msg>
  </logentry>
</log>

Obviously, that's much easier for a calling app to parse. If you need to bridge a cross-platform divide such that you can't just provide the backend class libraries, outputting to xml can be an easy fix.

tags:  

links: digg this    technorati