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:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
namespace XMLConverter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonBrowseSource_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
textBoxSource.Text = ofd.FileName;
}
private void buttonBrowseDest_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() == DialogResult.OK)
textBoxDest.Text = sfd.FileName;
}
private void buttonStart_Click(object sender, EventArgs e)
{
string xml;
try
{
using (StreamReader sr = new StreamReader(textBoxSource.Text))
{
xml = sr.ReadToEnd();
}
}
catch (Exception ex)
{
MessageBox.Show("Fehler beim Lesen der Datei:\n\n" + ex);
return;
}
if (xml.Contains("</RouteModelType>"))
{
Format1ToFormat2(xml);
}
else
{
Format2ToFormat1(xml);
}
}
private void Format2ToFormat1(string xml)
{
XmlDocument input = new XmlDocument();
XmlDocument output = new XmlDocument();
XmlDeclaration xmlDeclaration = output.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlElement root = output.DocumentElement;
output.InsertBefore(xmlDeclaration, root);
XmlElement routeModelTypeNode = output.CreateElement("RouteModelType");
XmlAttribute xsdAttribute = output.CreateAttribute("xmlns:xsd");
xsdAttribute.Value = "http://www.w3.org/2001/XMLSchema";
XmlAttribute xsiAttribute = output.CreateAttribute("xmlns:xsi");
xsiAttribute.Value = "http://www.w3.org/2001/XMLSchema-instance";
routeModelTypeNode.Attributes.Append(xsdAttribute);
routeModelTypeNode.Attributes.Append(xsiAttribute);
output.InsertBefore(xmlDeclaration, root);
output.AppendChild(routeModelTypeNode);
input.LoadXml(xml);
string routeName = input.DocumentElement.ChildNodes[0].Attributes[1].Value;
XmlElement routeNameNode = output.CreateElement("Name");
routeNameNode.InnerText = routeName;
routeModelTypeNode.AppendChild(routeNameNode);
XmlElement notesNode = output.CreateElement("Notes");
routeModelTypeNode.AppendChild(notesNode);
foreach (XmlNode node in input.GetElementsByTagName("waypoint"))
{
routeModelTypeNode.AppendChild(createWayPoint(output, node));
}
try
{
output.Save(textBoxDest.Text);
MessageBox.Show("erledigt!");
}
catch (Exception ex)
{
MessageBox.Show("Fehler beim Schreiben der Datei:\n\n" + ex);
}
}
private XmlElement createWayPoint(XmlDocument document, XmlNode node)
{
XmlElement result = document.CreateElement("Waypoints");
XmlElement waypointName = document.CreateElement("Name");
XmlElement latitude = document.CreateElement("Latitude");
XmlElement longitude = document.CreateElement("Longitude");
XmlElement notes = document.CreateElement("Notes");
XmlElement isParameterPoint = document.CreateElement("IsParameterPoint");
XmlElement sailMode = document.CreateElement("SailMode");
XmlElement isArrivalPoint = document.CreateElement("IsArrivalPoint");
XmlElement radius = document.CreateElement("Radius");
XmlElement trackLimit = document.CreateElement("TrackLimit");
XmlElement courseLimit = document.CreateElement("CourseLimit");
XmlElement economy = document.CreateElement("Economy");
XmlElement maximalSpeed = document.CreateElement("MaximalSpeed");
waypointName.InnerText = node.Attributes["name"].Value;
latitude.InnerText = convertCoordinate(node.Attributes["lat"].Value);
longitude.InnerText = convertCoordinate(node.Attributes["lon"].Value);
isParameterPoint.InnerText = "false";
sailMode.InnerText = "0";
isArrivalPoint.InnerText = "false";
radius.InnerText = "463";
trackLimit.InnerText = "1000";
courseLimit.InnerText = "0";
economy.InnerText = "0";
maximalSpeed.InnerText = "0";
result.AppendChild(waypointName);
result.AppendChild(latitude);
result.AppendChild(longitude);
for (int i = 0; i < 5; i++)
{
result.AppendChild(i==0?notes:notes.Clone());
}
result.AppendChild(isParameterPoint);
result.AppendChild(sailMode);
result.AppendChild(isArrivalPoint);
result.AppendChild(radius);
result.AppendChild(trackLimit);
result.AppendChild(courseLimit);
result.AppendChild(economy);
result.AppendChild(maximalSpeed);
return result;
}
private string convertCoordinate(String coordinate)
{
Double result = Double.Parse(coordinate, CultureInfo.InvariantCulture);
result *= Math.PI / 180;
return "" + result.ToString(CultureInfo.InvariantCulture);
}
private void Format1ToFormat2(string xml)
{
xml = xml.Remove(0, xml.IndexOf("<Name>") + 6);
string name = xml.Substring(0, xml.IndexOf("</Name>"));
StringBuilder output = new StringBuilder();
output.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
output.AppendLine();
output.AppendLine("<sposroutetemplate xmlns=\"http://www.meteogroup-maritime.com/spos/routetemplate\" xmlVersion=\"1\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
output.AppendLine();
output.AppendLine();
output.AppendLine("<template created=\"2015-02-23T14:13:58.5077114Z\" name=\"" + name + "\">");
output.AppendLine();
output.AppendLine();
output.AppendLine("<waypoints>");
output.AppendLine();
while (xml.IndexOf("</Waypoints>") > 0)
{
xml = xml.Remove(0, xml.IndexOf("<Name>") + 6);
name = xml.Substring(0, xml.IndexOf("</Name>"));
xml = xml.Remove(0, xml.IndexOf("<Latitude>") + 10);
double latitude = double.Parse(xml.Substring(0, xml.IndexOf("</Latitude>")), CultureInfo.InvariantCulture) * 180 / Math.PI;
xml = xml.Remove(0, xml.IndexOf("<Longitude>") + 11);
double longitude = double.Parse(xml.Substring(0, xml.IndexOf("</Longitude>")), CultureInfo.InvariantCulture) * 180 / Math.PI;
xml = xml.Remove(0, xml.IndexOf("</Waypoints>") + 12);
output.AppendLine("<waypoint name=\"" + name + "\" ignoreLand=\"false\" routeTemplatePointType=\"Fixed\" tracktype=\"RhumbLine\" useSpeed=\"false\" speed=\"0\" delay=\"PT0S\" lon=\"" + longitude.ToString(CultureInfo.InvariantCulture) + "\" lat=\"" + latitude.ToString(CultureInfo.InvariantCulture) + "\"/>");
output.AppendLine();
}
output.AppendLine("</waypoints>");
output.AppendLine();
output.AppendLine("</template>");
output.AppendLine();
output.AppendLine("</sposroutetemplate>");
try
{
using (StreamWriter sw = new StreamWriter(textBoxDest.Text))
sw.Write(output.ToString());
MessageBox.Show("erledigt!");
}
catch (Exception ex)
{
MessageBox.Show("Fehler beim Schreiben der Datei:\n\n" + ex);
}
}
}
}
|