From ee99cbcd28dec6f966ae154024c2cdf150ad3e9c Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 27 May 2016 14:27:56 +0530 Subject: [PATCH] Created a utility to convert any XML file into json format. --- .gitignore | 29 +++++++++++++++++++++++++++++ .tfignore | 1 + XMLtoJSON_utility.sln | 22 ++++++++++++++++++++++ XMLtoJSON_utility/Default.aspx | 40 ++++++++++++++++++++++++++++++++++++++++ XMLtoJSON_utility/Default.aspx.cs | 176 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ XMLtoJSON_utility/Default.aspx.designer.cs | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ XMLtoJSON_utility/Properties/AssemblyInfo.cs | 35 +++++++++++++++++++++++++++++++++++ XMLtoJSON_utility/Upload/da_dat_bgart.xml | 25 +++++++++++++++++++++++++ XMLtoJSON_utility/Upload/da_dat_brview.xml | 30 ++++++++++++++++++++++++++++++ XMLtoJSON_utility/Upload/da_dat_contentlist.xml | 18 ++++++++++++++++++ XMLtoJSON_utility/Web.Debug.config | 30 ++++++++++++++++++++++++++++++ XMLtoJSON_utility/Web.Release.config | 31 +++++++++++++++++++++++++++++++ XMLtoJSON_utility/Web.config | 14 ++++++++++++++ XMLtoJSON_utility/XMLtoJSON_utility.csproj | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ XMLtoJSON_utility/da_dat_brview.xml | 30 ++++++++++++++++++++++++++++++ 15 files changed, 668 insertions(+), 0 deletions(-) create mode 100644 .gitignore create mode 100644 .tfignore create mode 100644 XMLtoJSON_utility.sln create mode 100644 XMLtoJSON_utility/Default.aspx create mode 100644 XMLtoJSON_utility/Default.aspx.cs create mode 100644 XMLtoJSON_utility/Default.aspx.designer.cs create mode 100644 XMLtoJSON_utility/Properties/AssemblyInfo.cs create mode 100644 XMLtoJSON_utility/Upload/da_dat_bgart.xml create mode 100644 XMLtoJSON_utility/Upload/da_dat_brview.xml create mode 100644 XMLtoJSON_utility/Upload/da_dat_contentlist.xml create mode 100644 XMLtoJSON_utility/Web.Debug.config create mode 100644 XMLtoJSON_utility/Web.Release.config create mode 100644 XMLtoJSON_utility/Web.config create mode 100644 XMLtoJSON_utility/XMLtoJSON_utility.csproj create mode 100644 XMLtoJSON_utility/da_dat_brview.xml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..176c449 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +Thumbs.db +*.obj +*.exe +*.pdb +*.user +*.aps +*.pch +*.vspscc +*_i.c +*_p.c +*.ncb +*.suo +*.sln.docstates +*.tlb +*.tlh +*.bak +*.cache +*.ilk +*.log +[Bb]in +[Dd]ebug*/ +*.lib +*.sbr +obj/ +[Rr]elease*/ +_ReSharper*/ +[Tt]est[Rr]esult* +*.vssscc +$tf*/ \ No newline at end of file diff --git a/.tfignore b/.tfignore new file mode 100644 index 0000000..e37a9f1 --- /dev/null +++ b/.tfignore @@ -0,0 +1 @@ +\.git \ No newline at end of file diff --git a/XMLtoJSON_utility.sln b/XMLtoJSON_utility.sln new file mode 100644 index 0000000..f884d94 --- /dev/null +++ b/XMLtoJSON_utility.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.21005.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XMLtoJSON_utility", "XMLtoJSON_utility\XMLtoJSON_utility.csproj", "{5BFBC082-1D8E-45D3-B12F-264A7626F245}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5BFBC082-1D8E-45D3-B12F-264A7626F245}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5BFBC082-1D8E-45D3-B12F-264A7626F245}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5BFBC082-1D8E-45D3-B12F-264A7626F245}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5BFBC082-1D8E-45D3-B12F-264A7626F245}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/XMLtoJSON_utility/Default.aspx b/XMLtoJSON_utility/Default.aspx new file mode 100644 index 0000000..3be31c7 --- /dev/null +++ b/XMLtoJSON_utility/Default.aspx @@ -0,0 +1,40 @@ +<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="XMLtoJSON_utility.Default" ValidateRequest="false"%> + + + + + + + + +
+
+

Converting XML to JSON



+ + + + + + + + + +
Select File  

Upload XML:
+ +



JSON:
+ +
+ +
+
+
+ +
+
+ +
+ +
+
+ + diff --git a/XMLtoJSON_utility/Default.aspx.cs b/XMLtoJSON_utility/Default.aspx.cs new file mode 100644 index 0000000..2cd6201 --- /dev/null +++ b/XMLtoJSON_utility/Default.aspx.cs @@ -0,0 +1,176 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Web; +using System.Web.Script.Serialization; +using System.Web.UI; +using System.Web.UI.WebControls; +using System.Windows.Forms; +using System.Xml; +using System.Xml.Linq; + +namespace XMLtoJSON_utility +{ + public partial class Default : System.Web.UI.Page + { + protected void Page_Load(object sender, EventArgs e) + { + + } + + public string DefaultFileName = "Upload/"; + protected void UploadButton_Click(object sender, EventArgs e) + { + txtJson.Text = ""; + txtXml.Text = ""; + Label1.Text=""; + XmlDocument doc = new XmlDocument(); + + if (FileUploader.HasFile) + try + { + FileUploader.SaveAs(Server.MapPath(DefaultFileName) + + FileUploader.FileName); + string path = Server.MapPath("~/Upload/" + FileUploader.PostedFile.FileName); + doc.Load(path); + txtXml.Text = doc.InnerXml; + string JSON = XmlToJSON(doc); + txtJson.Text = JSON; + } + catch (Exception ex) + { + Label1.Text = "ERROR: " + ex.Message.ToString(); + } + else + { + Label1.Text = "You have not specified a file."; + } + + } + + private static string XmlToJSON(XmlDocument xmlDoc) + { + StringBuilder sbJSON = new StringBuilder(); + sbJSON.Append("{ "); + XmlToJSONnode(sbJSON, xmlDoc.DocumentElement, true); + sbJSON.Append("}"); + return sbJSON.ToString(); + } + + private static void XmlToJSONnode(StringBuilder sbJSON, XmlElement node, bool showNodeName) + { + if (showNodeName) + sbJSON.Append("\"" + SafeJSON(node.Name) + "\": "); + sbJSON.Append("{"); + + Dictionary childNodeNames = new Dictionary(); + string attrName; + + // Add in all nodes + foreach (XmlNode cnode in node.ChildNodes) + { + if (cnode is XmlText) + StoreChildNode(childNodeNames, "value", cnode.InnerText); + else if (cnode is XmlElement) + StoreChildNode(childNodeNames, cnode.Name, cnode); + } + // Add in all node attributes + if (node.Attributes != null) + foreach (XmlAttribute attr in node.Attributes) + { + attrName = "_" + attr.Name; + StoreChildNode(childNodeNames, attrName, attr.InnerText); + } + // Now output all stored info + foreach (string childname in childNodeNames.Keys) + { + ArrayList alChild = (ArrayList)childNodeNames[childname]; + if (alChild.Count == 1) + OutputNode(childname, alChild[0], sbJSON, true); + else + { + sbJSON.Append(" \"" + SafeJSON(childname) + "\": [ "); + foreach (object Child in alChild) + OutputNode(childname, Child, sbJSON, false); + sbJSON.Remove(sbJSON.Length - 2, 2); + sbJSON.Append(" ], "); + } + } + sbJSON.Remove(sbJSON.Length - 2, 2); + sbJSON.Append(" }"); + } + // StoreChildNode: Store data associated with each nodeName + // so that we know whether the nodeName is an array or not. + private static void StoreChildNode(Dictionary childNodeNames, string nodeName, object nodeValue) + { + if (nodeValue is XmlElement) + { + XmlNode cnode = (XmlNode)nodeValue; + if (cnode.Attributes.Count == 0) + { + XmlNodeList children = cnode.ChildNodes; + if (children.Count == 0) + nodeValue = null; + else if (children.Count == 1 && (children[0] is XmlText)) + nodeValue = ((XmlText)(children[0])).InnerText; + } + } + + // object oValuesAL = childNodeNames[nodeName]; + ArrayList ValuesAL; + if (!childNodeNames.ContainsKey(nodeName)) + { + ValuesAL = new ArrayList(); + childNodeNames[nodeName] = ValuesAL; + } + else + ValuesAL = childNodeNames[nodeName]; + ValuesAL.Add(nodeValue); + } + + private static void OutputNode(string childname, object alChild, StringBuilder sbJSON, bool showNodeName) + { + if (alChild == null) + { + if (showNodeName) + sbJSON.Append("\"" + SafeJSON(childname) + "\": "); + sbJSON.Append("null"); + } + else if (alChild is string) + { + if (showNodeName) + sbJSON.Append("\"" + SafeJSON(childname) + "\": "); + string sChild = (string)alChild; + sChild = sChild.Trim(); + sbJSON.Append("\"" + SafeJSON(sChild) + "\""); + } + else + XmlToJSONnode(sbJSON, (XmlElement)alChild, showNodeName); + sbJSON.Append(", "); + } + + // Make a string safe for JSON + private static string SafeJSON(string sIn) + { + StringBuilder sbOut = new StringBuilder(sIn.Length); + foreach (char ch in sIn) + { + if (Char.IsControl(ch) || ch == '\'') + { + int ich = (int)ch; + sbOut.Append(@"\u" + ich.ToString("x4")); + continue; + } + else if (ch == '\"' || ch == '\\' || ch == '/') + { + sbOut.Append('\\'); + } + sbOut.Append(ch); + } + return sbOut.ToString(); + } + } +} \ No newline at end of file diff --git a/XMLtoJSON_utility/Default.aspx.designer.cs b/XMLtoJSON_utility/Default.aspx.designer.cs new file mode 100644 index 0000000..625173d --- /dev/null +++ b/XMLtoJSON_utility/Default.aspx.designer.cs @@ -0,0 +1,69 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace XMLtoJSON_utility { + + + public partial class Default { + + /// + /// form1 control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.HtmlControls.HtmlForm form1; + + /// + /// FileUploader control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.FileUpload FileUploader; + + /// + /// txtXml control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.TextBox txtXml; + + /// + /// UploadButton control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.Button UploadButton; + + /// + /// txtJson control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.TextBox txtJson; + + /// + /// Label1 control. + /// + /// + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// + protected global::System.Web.UI.WebControls.Label Label1; + } +} diff --git a/XMLtoJSON_utility/Properties/AssemblyInfo.cs b/XMLtoJSON_utility/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..2f7742e --- /dev/null +++ b/XMLtoJSON_utility/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("XMLtoJSON_utility")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("XMLtoJSON_utility")] +[assembly: AssemblyCopyright("Copyright © 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("93f41f32-37a7-4083-8a3e-83fa608ba870")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Revision and Build Numbers +// by using the '*' as shown below: +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/XMLtoJSON_utility/Upload/da_dat_bgart.xml b/XMLtoJSON_utility/Upload/da_dat_bgart.xml new file mode 100644 index 0000000..ad043bf --- /dev/null +++ b/XMLtoJSON_utility/Upload/da_dat_bgart.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/XMLtoJSON_utility/Upload/da_dat_brview.xml b/XMLtoJSON_utility/Upload/da_dat_brview.xml new file mode 100644 index 0000000..c673fb0 --- /dev/null +++ b/XMLtoJSON_utility/Upload/da_dat_brview.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/XMLtoJSON_utility/Upload/da_dat_contentlist.xml b/XMLtoJSON_utility/Upload/da_dat_contentlist.xml new file mode 100644 index 0000000..fde5b73 --- /dev/null +++ b/XMLtoJSON_utility/Upload/da_dat_contentlist.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/XMLtoJSON_utility/Web.Debug.config b/XMLtoJSON_utility/Web.Debug.config new file mode 100644 index 0000000..2e302f9 --- /dev/null +++ b/XMLtoJSON_utility/Web.Debug.config @@ -0,0 +1,30 @@ + + + + + + + + + + \ No newline at end of file diff --git a/XMLtoJSON_utility/Web.Release.config b/XMLtoJSON_utility/Web.Release.config new file mode 100644 index 0000000..c358444 --- /dev/null +++ b/XMLtoJSON_utility/Web.Release.config @@ -0,0 +1,31 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/XMLtoJSON_utility/Web.config b/XMLtoJSON_utility/Web.config new file mode 100644 index 0000000..d78bd2b --- /dev/null +++ b/XMLtoJSON_utility/Web.config @@ -0,0 +1,14 @@ + + + + + + + + + + + diff --git a/XMLtoJSON_utility/XMLtoJSON_utility.csproj b/XMLtoJSON_utility/XMLtoJSON_utility.csproj new file mode 100644 index 0000000..8196c00 --- /dev/null +++ b/XMLtoJSON_utility/XMLtoJSON_utility.csproj @@ -0,0 +1,118 @@ + + + + + Debug + AnyCPU + + + 2.0 + {5BFBC082-1D8E-45D3-B12F-264A7626F245} + {349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc} + Library + Properties + XMLtoJSON_utility + XMLtoJSON_utility + v4.5.2 + true + + + + + + + true + full + false + bin\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Default.aspx + ASPXCodeBehind + + + Default.aspx + + + + + + Web.config + + + Web.config + + + + + + + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + + + + + + True + True + 50537 + / + http://localhost:50537/ + False + False + + + False + + + + + + \ No newline at end of file diff --git a/XMLtoJSON_utility/da_dat_brview.xml b/XMLtoJSON_utility/da_dat_brview.xml new file mode 100644 index 0000000..c673fb0 --- /dev/null +++ b/XMLtoJSON_utility/da_dat_brview.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file -- libgit2 0.21.4