I'm trying the parse the xml file at this link: http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.quakeml.
The problem I am having is that the foreach
loop itself is not executing. I think its a problem with namespaces in the xml document.
Below is my code:
protected void btnStoreXMLData_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["EarthquakeCS"].ConnectionString);
SqlCommand cmd = new SqlCommand();
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("~/App_Data/all_hour.xml"));
XmlNamespaceManager nmspc = new XmlNamespaceManager(xmlDoc.NameTable);
nmspc.AddNamespace("", "http://quakeml.org/xmlns/bed/1.2");
nmspc.AddNamespace("catalog", "http://anss.org/xmlns/catalog/0.1");
nmspc.AddNamespace("q", "http://quakeml.org/xmlns/quakeml/1.2");
XmlNodeList dataNodes = xmlDoc.SelectNodes("/quakeml/eventParameters/event", nmspc);
//ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('test0');", true);
foreach (XmlNode node in dataNodes)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('test1');", true);
string location = (node.SelectSingleNode("description/text") != null) ? node.SelectSingleNode("description/text").InnerText.ToString() : string.Empty;
string time = (node.SelectSingleNode("origin/time/value") != null) ? node.SelectSingleNode("origin/time/value").InnerText.ToString() : string.Empty;
string longitude = (node.SelectSingleNode("origin/longitude/value") != null) ? node.SelectSingleNode("origin/longitude/value").InnerText.ToString() : string.Empty;
string latitude = (node.SelectSingleNode("origin/latitude/value") != null) ? node.SelectSingleNode("origin/latitude/value").InnerText.ToString() : string.Empty;
string depth = (node.SelectSingleNode("origin/depth/value") != null) ? node.SelectSingleNode("origin/depth/value").InnerText.ToString() : string.Empty;
string magnitude = (node.SelectSingleNode("magnitude/mag/value") != null) ? node.SelectSingleNode("magnitude/mag/value").InnerText.ToString() : string.Empty;
string magnitudeType = (node.SelectSingleNode("magnitude/type/") != null) ? node.SelectSingleNode("magnitude/type/").InnerText.ToString() : string.Empty;
cmd.CommandText = "INSERT INTO tblEarthquake (Location,Time,Latitude,Longitude,Depth,Magnitude,MagnitudeType) "
+ "VALUES (@Location,@Time,@Latitude,@Longitude,@Depth,@Magnitude,@MagnitudeType)";
cmd.CommandType = CommandType.Text;
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@Location", location);
cmd.Parameters.AddWithValue("@Time", time);
cmd.Parameters.AddWithValue("@Latitude", latitude);
cmd.Parameters.AddWithValue("@Longitude", longitude);
cmd.Parameters.AddWithValue("@Depth", depth);
cmd.Parameters.AddWithValue("@Magnitude", magnitude);
cmd.Parameters.AddWithValue("@MagnitudeType", magnitudeType);
cmd.Connection = con;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
//XML data has been inserted
}
else
{
//XML data has not been inserted
}
}
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true);
return;
}
finally
{
con.Close();
cmd.Dispose();
}
}`