In this post, I would like to document a couple of classes with several methods that are useful to convert from one type of data to another. For example, some of the methods can be used to convert a Dataset into JSON string and vice versa.
SQL Query output as XML
The first class to start with the most simplest one. This class can be used to wrap your SQL queries for Sql Server or Oracle such that they return XML output instead of rows. Strategy pattern has been used to implement these methods – nothing fancy.
The base class for query wrapper.
Then one wrapper each for Sql and Oracle has been implemented as shown.
And then there is services class that uses strategy pattern.
The following class provides methods for conversion between
1. XML to JSON
2. JSON to XML
3. Object to JSON
4. JSON to Object
5. Dataset to JSON
6. JSON to Dataset
7. XDocument to XmlDocument
8. XmlDocument to XDocument
9. Some dummy conversions to size In MB, time in seconds which might not be of much interest to you all.
For JSON methods, you would require a reference to JSON.NET libraries.
Test Data Helpers
In this set of helper classes, I wrote a couple of methods which wrap the most required functionality (at least for me) like generating a random string, getting a random integer, timing an execution. May or may not be useful to you all.
Simplified Xml querying using Extension Methods.
The following method returns all the elements in the given document (XDocument) with the passed ElementName and which have the specified attribute name and value. I use it a lot for automating my XML documents manipulation.
Below are few more helper methods which I use regularly. One of them is the GetCopy() method which returns a copy of the passed XElement.
I hope these utility methods provides good information about JSON, XML, XLinq, etc. Let me know if you have any comments to share.