First of all I would like to thank you for the support. I am new in coding and need assistance in getting a specific xml content to an excel spreadsheet or to a datagridview.
the XML file is not a standard one (i think and below is a sample).
what i need from this xml file is the following data:
the name (Towers xxx] of the alert and the gps points (dont need the utm)
this is the xml
[code]
SF Zones
1303889594
Tower 1
11111
2
false
true
true
true
0
0
23
59
0
98.8889
2
99
205
99
true
-1
C:\4C\RunTime\OWP\Sounds\sirenwawa.wav
40
22
5
1303890003
Towers 1 to 4
Fence Alert
11111
2
false
true
true
true
0
0
23
59
0
98.8889
2
99
204
99
205
99
true
-1
C:\4C\RunTime\OWP\Sounds\sirenwawa.wav
40
22
6
[/code]
thanks again
VulpesPosted Sep 6, 2011, 11:06 AM
You might be getting everything twice because the StreamWriter is appending to rather than overwriting the existing file, so I'd change that as well:
theWriter = new StreamWriter(path + fileName, false); // now overwriting rather than appending
//creating the KML File
theWriter.WriteLine("");
theWriter.WriteLine("
theWriter.WriteLine("
theWriter.WriteLine("
theWriter.WriteLine("
theWriter.WriteLine("
theWriter.WriteLine("
theWriter.WriteLine("
theWriter.WriteLine("");
// theWriter.WriteLine("");
theWriter.WriteLine("");
theWriter.WriteLine("
theWriter.WriteLine("
theWriter.WriteLine("
foreach (XElement geo in alert.Element("shape").Elements("geo_position"))
{
theWriter.WriteLine("
double latitude = (double)geo.Attribute("lat");
double longitude = (double)geo.Attribute("long");
// theWriter.WriteLine(String.Format("lat=\"{0}\" long=\"{1}\"", latitude, longitude));
theWriter.WriteLine(longitude + "," + latitude + ",23");
theWriter.WriteLine("");
}
theWriter.WriteLine("
}
catch (Exception ex)
{
j kPosted Sep 6, 2011, 3:49 AM
below is an example.
any idea why?
[code]
40.76876678920284,-73.98097831075269,23
40.76869191861909,-73.98078254132794,23
40.76839192572469,-73.97999998924834,23
40.76749000414105,-73.97804528571041,23
40.76643902004108,-73.97593868227233,23
40.76876678920284,-73.98097831075269,23
40.76869191861909,-73.98078254132794,23
40.76839192572469,-73.97999998924834,23
40.76749000414105,-73.97804528571041,23
40.76643902004108,-73.97593868227233,23
[/code]
And this is what i have in the putton
[code]
{
{
FileTypeExt.Text =
}
{
private void button1_Click(object sender, EventArgs e)if ((alertlayertextbox.Text == null) || (alertlayertextbox.Text == ""))MessageBox.Show("No Alert Layer File selected" + "\r\n" + "Please select an Alert Layer", "Error loading Alert Layer");null;return;XDocument doc = XDocument.Load(alertlayertextbox.Text);string path = outputfiletextbox.Text + "\\";foreach (XElement alert in doc.Root.Elements("alert_area_object"))StreamWriter theWriter = null;try{
string name = (string)alert.Element("name");string fileName = name.Trim() + FileTypeExt.Text;// ".kml";theWriter =
string polyname = name.Trim();new StreamWriter(path + fileName, true); // appends rather than overwrites?
//creating the KML FiletheWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
{
" + "\"" + "1.0" + "\"" + " encoding=" + "\"" + "UTF-8" + "\"" + "?>");"theWriter.WriteLine(longitude +
}
theWriter.WriteLine(
}
{
"," + latitude + ",23");" \n \n \n \n \n \n ");catch (Exception ex)// MessageBox.Show(ex.Message);errorlogger.WriteLine(ex.Message);
}
StreamWriter errorlogger = new StreamWriter(path + "error log.txt");if (errorlogger != null) errorlogger.Close();finally{
if (theWriter != null) theWriter.Close();// MessageBox.Show(FileTypeExt.Text);}
}
}
[/code]
VulpesPosted Sep 5, 2011, 3:52 PM
j kPosted Sep 5, 2011, 2:33 AM
thanks alot.
if i need these two lines to close the xml/kml file where should they be?
theWriter.WriteLine("");
theWriter.WriteLine(" \n \n \n \n \n ");
regarding the file extention it was a stupid mistake (should have always been .csv/.kml)
i am now only missing the issue with these two lines.
thanks again for the support.
VulpesPosted Sep 4, 2011, 3:00 PM
j kPosted Sep 4, 2011, 10:42 AM
sorry about that.
below you can find the entire code
please check this also - i have a combobox that includes insite (*.kml and *.csv).
when i choose that the streamwriter will use it "FileTypeExt.Text;" the software hangs and crashes (vshost.exe not working).
with the code below i get also the error message with the null and the vshost.exe hanging.
thanks again for the support.
[code]
using
System;using
System.Collections.Generic;using
System.ComponentModel;using
System.Data;using
System.Drawing;using
System.Text;using
System.Reflection;using
System.Runtime.InteropServices;using
System.IO;using
System.Xml;using
Excel = Microsoft.Office.Interop.Excel;using
System.Windows.Forms;using
System.Threading;using
System.Globalization;using
System.Xml.Linq;namespace
{
WindowsApplication2public partial class Form1 : Form{
{
InitializeComponent();
}
{
{
FileTypeExt.Text =
}
public Form1()private void button1_Click(object sender, EventArgs e)if ((alertlayertextbox.Text == null) || (alertlayertextbox.Text == ""))MessageBox.Show("No Alert Layer File selected" + "\r\n" + "Please select an Alert Layer", "Error loading Alert Layer");null;return;//if ((outputfiletextbox.Text == null) || (outputfiletextbox.Text == ""))
//{
// MessageBox.Show("Output Directory was not selected" + "\r\n" + "Please select output directory", "Error output directory can not be found");
// FileTypeExt.Text = null;
// return;
//}
XDocument doc = XDocument.Load(alertlayertextbox.Text);// string path = @"C:\Users\Desktop\Book Application\";
// string path = @"C:\Users\Desktop\Book Application\test\";
{
string path = outputfiletextbox.Text + "\\";foreach (XElement alert in doc.Root.Elements("alert_area_object"))StreamWriter theWriter = null;try{
string name = (string)alert.Element("name");string fileName = name.Trim() + FileTypeExt.Text;// ".kml";
theWriter =
string polyname = name.Trim();new StreamWriter(path + fileName, true); // appends rather than overwrites?
//creating the KML FiletheWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
" + "\"" + "1.0" + "\"" + " encoding=" + "\"" + "UTF-8" + "\"" + "?>");"
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
theWriter.WriteLine(
{
"");"
}
}
{
"," + latitude + ",23");catch (Exception ex)// MessageBox.Show(ex.Message);
// StreamWriter errorlogger = new StreamWriter(path + "error log.txt");
// errorlogger.WriteLine(ex.Message);
// if (errorlogger != null) errorlogger.Close();}
finally{
theWriter.WriteLine(
theWriter.WriteLine(
"");" \n \n \n \n \n ");if (theWriter != null) theWriter.Close();// MessageBox.Show(FileTypeExt.Text);}
}
}
{
{
outputfiletextbox.Text = outputlocation.SelectedPath;
}
}
{
alertlayerfiledialog.Filter =
{
alertlayertextbox.Text = alertlayerfiledialog.FileName;
}
}
{
}
{
{
CreateLayers.Enabled =
}
FileTypeExt.DropDownStyle =
}
{
{
CreateLayers.Enabled =
}
private void button3_Click(object sender, EventArgs e)if (outputlocation.ShowDialog() == DialogResult.OK)private void alertlayerbtn_Click(object sender, EventArgs e)"alert layer (*.xml) | *.xml";if (alertlayerfiledialog.ShowDialog() == DialogResult.OK)if (alertlayerfiledialog.FileName != " ")private void alertlayerfiledialog_FileOk(object sender, CancelEventArgs e)private void Form1_Load(object sender, EventArgs e)if ((FileTypeExt.Text == "") || (FileTypeExt.Text == null))false;ComboBoxStyle.DropDownList;private void FileTypeExt_SelectedIndexChanged(object sender, EventArgs e)if ((FileTypeExt.Text == "*.CSV") || (FileTypeExt.Text == "*.KML"))true;else{
CreateLayers.Enabled =
}
}
{
}
}
}
false;private void button1_Click_1(object sender, EventArgs e)MessageBox.Show(" + "\"" + "1.0" + "\"" + " encoding=" + "\"" + "UTF-8" + "\"" + "?>");MessageBox.Show("
[/code]
VulpesPosted Sep 3, 2011, 1:22 PM
j kPosted Sep 3, 2011, 7:24 AM
another small issue i am facing.
each time i run it i get the following error:
Object reference not set to an instance of an object.
and the vshost.exe stops working and the application crashes.
i have read a little about it and it called NullReferenceException
how do i avoid such.
for example if there is a null entry in the "alert_area_object" i want to ignore it.
thanks
VulpesPosted Aug 28, 2011, 6:15 PM
j kPosted Aug 28, 2011, 3:53 PM
now i have another small question. what i want is that for each alert.elemnt.name in the xml it will create a new file. the file name will be called after the alert.element.name (For example "Tower 1" the second file will be "Tower 1 to 4"). each file will include the geo location for each of the names.
In our case there should be two files
File #1
Fiel Name: Tower 1
content:
lat="40.76876678920284" long="-73.98097831075269"
lat="40.76869191861909" long="-73.98078254132794
lat="40.76839192572469" long="-73.97999998924834"
lat="40.76749000414105" long="-73.97804528571041"
lat="40.76643902004108" long="-73.97593868227233"
File #2
Fiel Name: Tower 1 to 4
content:
lat="40.76876678920284" long="-73.98097831075269"
lat="40.76869191861909" long="-73.98078254132794
lat="40.76839192572469" long="-73.97999998924834"
lat="40.76749000414105" long="-73.97804528571041"
lat="40.76643902004108" long="-73.97593868227233"
This is currently what i have. i took your code and just added the stream writer (currently writing all to one file).
can you please assist.
thanks again.
[code]
{
private void button1_Click(object sender, EventArgs e)XDocument doc = XDocument.Load(@alertlayertextbox.Text);{
StreamWriter theWriter = new System.IO.StreamWriter(@"C:\Users\Desktop\Book Application\monster.csv", true);foreach (XElement alert in doc.Root.Elements("alert_area_object"))try{
string Strpath = @"C:\Users\Desktop\Book Application\";string name = (string)alert.Element("name"); //working use this one
// theWriter.WriteLine("name = {0}", name.Trim()); //working use this onetheWriter.WriteLine(name.Trim());
{
theWriter.WriteLine(
}
}
{
}
}
theWriter.Close();
foreach (XElement geo in alert.Element("shape").Elements("geo_position"))double latitude = (double)geo.Attribute("lat");double longitude = (double)geo.Attribute("long");"lat = {0}, long = {1}", latitude, longitude);catch (NullReferenceException)//use this one}
[/code]
VulpesPosted Aug 23, 2011, 4:43 PM