1: public static class ConversionHelpers
2: {
3: #region Xml-X Document Conversions
4: /// <summary>
5: /// Converts XmlNode into an XDocument.
6: /// </summary>
7: /// <param name="doc">The XMLDocument to be converted</param>
8: /// <returns>The XDocument generated</returns>
9: public static XDocument AsXDocument(this XmlNode doc)
10: {
11: return XDocument.Load(new XmlNodeReader(doc));
12: }
13:
14: /// <summary>
15: /// converts the XNode into XmlDocument
16: /// </summary>
17: /// <param name="xdoc">The xdoc.</param>
18: /// <returns>the xml document after conversion</returns>
19: public static XmlDocument AsXmlDocument(this XNode xdoc)
20: {
21: var doc = new XmlDocument();
22: doc.Load(xdoc.CreateReader());
23: return doc;
24: }
25: #endregion
26:
27: #region Xml To JSON
28: /// <summary>
29: /// Converts the XNode passed into JSON String
30: /// </summary>
31: /// <param name="doc">Xdocument to be converted</param>
32: /// <returns>JSON String</returns>
33: public static string AsJsonString(this XNode doc)
34: {
35: return JavaScriptConvert.SerializeXmlNode(doc.AsXmlDocument());
36: }
37:
38: /// <summary>
39: /// converts the XML passed into json string.
40: /// </summary>
41: /// <param name="xml">The XML.</param>
42: /// <returns>JSON String notation</returns>
43: public static string AsJsonString(this string xml)
44: {
45: return XDocument.Parse(xml).AsJsonString();
46: }
47:
48: /// <summary>
49: /// Opens the XML file mentioned in "fi" and return it as JSON String.
50: /// </summary>
51: /// <param name="fi"></param>
52: /// <returns></returns>
53: public static string GetJsonContent(this FileInfo fi)
54: {
55: if (File.Exists(fi.FullName))
56: return XDocument.Load(fi.FullName).AsJsonString();
57: else
58: {
59: return null;
60: }
61: }
62:
63: #endregion
64:
65: #region Json to XML
66: /// <summary>
67: /// Converts the json string into XML
68: /// </summary>
69: /// <param name="json">JSON as string</param>
70: /// <returns>JSON returned as XML</returns>
71: public static string AsXml(this string json)
72: {
73: return JavaScriptConvert.DeserializeXmlNode(json).InnerXml;
74: }
75: #endregion
76:
77: #region Object <-> JSON
78: public static string ToJSON<T>(this T obj)
79: {
80: return JavaScriptConvert.SerializeObject(obj);
81: }
82:
83: public static T FromJSON<T>(this string json)
84: {
85: return JavaScriptConvert.DeserializeObject<T>(json);
86: }
87: #endregion
88:
89: #region Dataset <--> JSON
90: public static string ToJSON(this DataSet ds)
91: {
92: return ds.GetXml().AsJsonString();
93: }
94:
95: public static DataSet ToDataset(this string jsonString, bool isXml)
96: {
97: var ds = new DataSet();
98: string asXml = string.Empty;
99: if (!isXml)
100: asXml = jsonString.AsXml();
101: else
102: {
103: asXml = jsonString;
104: }
105: using (var sr = new StringReader(asXml))
106: {
107: ds.ReadXml(sr);
108: }
109: return ds;
110: }
111:
112: #endregion
113:
114: #region Others....
115: /// <summary>
116: /// Computes the size of the byte array in Megabytes
117: /// </summary>
118: /// <param name="array">byte array to be computed</param>
119: /// <returns>size of the array in MB</returns>
120: public static double SizeInMB(this byte[] array)
121: {
122: return array.Length / (1024 * 1024);
123: }
124:
125: /// <summary>
126: /// Converst the value into seconds. Basically it divides by 1000.
127: /// </summary>
128: /// <param name="val">Value in Milliseconds</param>
129: /// <returns>Value in seconds</returns>
130: public static double TimeInSeconds(this double val)
131: {
132: return val / (1000);
133: }
134:
135: #endregion
136: }