Utility.cs 7.63 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ionic.Zip;
using System.Reflection;
using System.IO;
using System.Xml.Linq;
using System.Xml;

namespace ModifyXml
{
    class Utility
    {

        public static string GetAssemblyPath()
        {
            Assembly ass = Assembly.GetExecutingAssembly();
            string appPath = System.IO.Path.GetDirectoryName(ass.Location);

            return appPath;
        }
        public void RenameAtlasAnatomyXmlNodes(string xmlFilePath)
        {
            try
            {
                var document = XDocument.Load(xmlFilePath);
                var query2 = from c2 in document.Descendants("root").Elements("it")
                             select c2;
                try
                {
                    foreach (XElement layer2 in query2)
                    {
                        if (layer2.Attribute("pId").Value != "")
                        {
                            //to Change attributes names first get value 
                            var att_pId_value = layer2.Attribute("pId").Value;
                            var att_hX_value = layer2.Attribute("hX").Value;
                            var att_hY_value = layer2.Attribute("hY").Value;
                            var att_pX_value = layer2.Attribute("pX").Value;
                            var att_pY_value = layer2.Attribute("pY").Value;
                            var att_tId_value = layer2.Attribute("tId").Value;
                            var att_bsId_value = layer2.Attribute("bsId").Value;
                            var att_bs_value = layer2.Attribute("bs").Value;

                            //remove attribute we want to rename
                            layer2.Attribute("pId").Remove();
                            layer2.Attribute("hX").Remove();
                            layer2.Attribute("hY").Remove();
                            layer2.Attribute("pX").Remove();
                            layer2.Attribute("pY").Remove();
                            layer2.Attribute("tId").Remove();
                            layer2.Attribute("bsId").Remove();
                            layer2.Attribute("bs").Remove();

                            //add renamed attributes 
                            layer2.SetAttributeValue("PinId", att_pId_value);
                            layer2.SetAttributeValue("HeadX", att_hX_value);
                            layer2.SetAttributeValue("HeadY", att_hY_value);
                            layer2.SetAttributeValue("PinX", att_pX_value);
                            layer2.SetAttributeValue("PinY", att_pY_value);
                            layer2.SetAttributeValue("TermId", att_tId_value);
                            layer2.SetAttributeValue("BodySystemId", att_bsId_value);
                            layer2.SetAttributeValue("BodySystemName", att_bs_value);

                            //rename the element it
                            XName newElemName = "Item";
                            layer2.Name = newElemName;
                        }
                    }
                    var doc2root = document.Element("root");
                    var att_aoi_value = doc2root.Attribute("aoi").Value;
                    doc2root.Attribute("aoi").Remove();
                    doc2root.SetAttributeValue("NavigatorImage", att_aoi_value);
                    XName newdoc2root = "BodyRegionViews";
                    doc2root.Name = newdoc2root;

                    string fileName = Path.GetFileName(xmlFilePath);
                    string appPath = Utility.GetAssemblyPath();
                    int strLen = appPath.Length;
                    string modifiedXmlLocation = appPath.Substring(0, (appPath.IndexOf("\\bin"))) + "\\modifiedxml";
                    if (Directory.Exists(modifiedXmlLocation))
                    {
                        document.Save(modifiedXmlLocation + "\\" + fileName);
                        ParseXMLtoJson(modifiedXmlLocation + "\\" + fileName);
                    }
                    else
                    {
                        Directory.CreateDirectory(modifiedXmlLocation);
                        document.Save(modifiedXmlLocation + "\\" + fileName);
                        ParseXMLtoJson(modifiedXmlLocation + "\\" + fileName);
                    }
                }
                catch (NullReferenceException ex)
                {
                    ex.ToString();
                }
            }
            catch (XmlException exp)
            {
                exp.ToString();
            }
        }
        public void ParseXMLtoJson(string xmlFilePath)
        {
            string appPath = Utility.GetAssemblyPath();
            int strLen = appPath.Length;
            //string xpath = @"D:\C# EXERERCISES\WINDOWS\modifiedxml\aa_dat_pindata_2862.xml";
            var document = XDocument.Load(xmlFilePath);
            string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(document);

            string[] splitPath = xmlFilePath.Split('\\');
            string f = (splitPath[splitPath.Length - 1]).Substring(0, (splitPath[splitPath.Length - 1]).IndexOf('.'));
            string fileName = (splitPath[splitPath.Length - 1]).Substring(0, (splitPath[splitPath.Length - 1]).IndexOf('.'));

            string newLoc = appPath.Substring(0, (appPath.IndexOf("\\bin"))) + "\\aa_dat_pindata_json";
            string fileNewName = newLoc + "\\" + fileName + ".json";
            if (Directory.Exists(newLoc)) { }
            else
                Directory.CreateDirectory(newLoc);

            using (FileStream fs = File.Create(fileNewName))
            {
                Byte[] info = new UTF8Encoding(true).GetBytes(jsonString);
                // Add some information to the file.
                fs.Write(info, 0, info.Length);
            }

            File.Delete(xmlFilePath); //Clear disk space...
        }


        public static void ConvertAtlasAnatomyXMLToJSON(string zippedfolderPath)
        {

            Utility objUtility = new Utility();

            string appPath = Utility.GetAssemblyPath();

            string extractLocation = appPath + "\\ExtractedFiles";
            if (!Directory.Exists(extractLocation))
            {
                Directory.CreateDirectory(appPath);
            }

            using (ZipFile zip = ZipFile.Read(zippedfolderPath))
            {
                zip.ExtractAll(extractLocation, ExtractExistingFileAction.DoNotOverwrite);

                bool res = !Directory.EnumerateFiles(appPath).Any();

                if ((Directory.GetFiles(extractLocation).Length == 0) && (System.IO.Directory.GetDirectories(extractLocation).Length > 0))
                {
                    DirectoryInfo directory = new DirectoryInfo(extractLocation);
                    DirectoryInfo[] subdirs = directory.GetDirectories();
                    if (subdirs.Length > 0)
                        extractLocation += "/" + subdirs[0].Name;

                    if (Directory.GetFiles(extractLocation).Length > 0)
                    {
                        foreach (string file in Directory.EnumerateFiles(extractLocation, "*.xml"))
                        {
                            objUtility.RenameAtlasAnatomyXmlNodes(file);
                        }
                    }
                }
            }

            string modifiedXmlLocation = appPath.Substring(0, (appPath.IndexOf("\\bin"))) + "\\modifiedxml";
            Directory.Delete(modifiedXmlLocation, true); //free up disk space...
            string extractLoc = extractLocation.Substring(0, extractLocation.IndexOf("ExtractedFiles") + 14);
            Directory.Delete(extractLoc, true); //free up disk space...
        }
    }
}