Utility.cs
7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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...
}
}
}