KnowDotNet Visual Organizer

Parsing XML With ADO.NET DataSet ReadXML

An Alternate to XML Document Object

by Les Smith
Print this Article Discuss in Forums

There are several ways to "skin a cat" in .NET.  XML Parsing is painful at best and sometimes the XML Document Object is the most painful.  If you don't code to read and write XML every day, and I don't, then remembering all of the objects and their methods and things like XPath, etc, can be a real pain. ADO.NET gives you a simple way to parse XML, and DataSet ReadXML is easier than using the XML Document Object.

Let's say that in my application I have read an XML string from a database field and I need to parse it.  The following code will allow me to access the fields from a DataRow by name and I can forget that it ever was in XML.  Granted, the XML is very simple, but the object of the XML string is to show the use of ADO.NET to automatically parse your string.  

In the following code, I am using an often overlooked .NET object, the StringReader.  Also, althouth Intellisense on the ds.ReadXML does not show that StringReader is an overload parameter, it is.  Since I already have a string of XML, the StringReader is the obvious way to create a Stream.

   string xml = "<file><field1>Field1 Data<field2>2";

  
StringReader sr = new StringReader(xml);

  
DataSet ds = new DataSet();
   ds.ReadXml(sr);

  
DataRow dr = ds.Tables[0].Rows[0];

  
string s = string.Format("Field1: {0}", dr["field1"]);
  
Console.WriteLine(s);
   s =
string.Format("Field2: {0}", dr["field2"]);
  
Console.WriteLine(s);

Running the code shown above in a Console Application will produce the following results in the Console window.

    Field1: Field1 Data
    Field2: 2


Someone might say there is a lot of overhead with a DataSet, but the XML Document reader is not the fastest thing in the world either.  So WAHT!  I am a pragmatist when it comes to programming.  If it works and saves developer time, given other things being approximately equal, I am for the easy to read, easy to use approach way every time.  I used to program in VB.NET exclusively.  Now, due to my employer's choice, I code almost exclusively in C#.  But, if I come upon a problem that is easier to solve for me in VB.NET, I have absolutely no qualms about coding a DLL in VB.NET.  Programmers should recognize that they are paid for production, not semantics.  There's my oft repeated philosophy for this article!  

Need to automatically organize your code windows?  You'll be amazed how easy it is to keep the code in your code windows organized.  TRY IT FREE FOR 30 DAYS BY CLICKING HERE.



Automatically generate braces in C#! Try CSharpCompleter and stop wasting valuable time needlessly typing hundreds of braces {} daily.  Try CSharpCompleter for 30 DAYS FREE.



Ask a Question, or give your feedback on my articles or products by going to the KnowDotNet Forum or by clicking on My Blog.
  

You can also email me directly at les@KnowDotNet.com
(Almost all things Dot Net).

Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing