我知道如何将XML数据加载到字符串中但我无法将其内容(XML文件节点中的内容)存储到我的数据库中。我已经尝试了很多解决方案,但没有一个在我的情况下工作。 XML链接是: -
http://timesofindia.indiatimes.com/rssfeedstopstories.cms
我只想保存XML文件节点中存在的数据,而XML文件来自URL。
使用(var client = new WebClient()) { string xmlData = new System.Net.WebClient()。DownloadString(“http://timesofindia.indiatimes.com/rssfeedstopstories.cms”); XmlDocument xml = new XmlDocument(); XmlNodeList nodeList = xml.SelectNodes(“/ rss / channel / item”);
foreach (XmlNode node in nodeList)
{
var title = node["title"].InnerText;
var description = node["description"].InnerText;
var link = node["link"].InnerText;
var guid = node["guid"].InnerText;
var pubdate = node["pubDate"].InnerText;
SqlConnection cnn = new SqlConnection(@"Data Source=vishal-pc\mssqlserver2012;Initial Catalog=aditya;User ID=sa;password=1234");
SqlCommand cmd = new SqlCommand("insert into XMLdata (title,description,link,guid,pubdate) VALUES (@xmlData,@xmlData1,@xmlData2,@xmlData3,@xmlData4)", cnn);
cnn.Open();
//string sqlForInsert = "INSERT INTO XMLdata (title,description,link,guid,pubdate) VALUES (@xmlData,@xmlData1,@xmlData2,@xmlData3,@xmlData4);";
int rowsAffected = 0;
//try
//{
// using (SqlConnection con = new SqlConnection(@"Data Source=vishal-pc\mssqlserver2012;Initial Catalog=aditya;User ID=sa;password=1234"))
// using (SqlCommand cmd = new SqlCommand(sqlForInsert, con))
{
cmd.Parameters.Add(new SqlParameter("@xmlData", title));
cmd.Parameters.Add(new SqlParameter("@xmlData1", description));
cmd.Parameters.Add(new SqlParameter("@xmlData2", link));
cmd.Parameters.Add(new SqlParameter("@xmlData3", guid));
cmd.Parameters.Add(new SqlParameter("@xmlData4", pubdate));
rowsAffected = cmd.ExecuteNonQuery();
cnn.Close();
}
答案 0 :(得分:0)
尝试此 您需要做的所有事情:
添加其他参数
string xmlData = new System.Net.WebClient().DownloadString("http://timesofindia.indiatimes.com/rssfeedstopstories.cms");
string sqlForInsert = "INSERT INTO TableName (xmlDataField) VALUES (@xmlData);";
int rowsAffected = 0;
try
{
using (SqlConnection con = new SqlConnection("<ConnectionString>"))
using (SqlCommand cmd = new SqlCommand(sqlForInsert, con))
{
cmd.Parameters.Add(new SqlParameter("xmlData", xmlData));
con.Open();
rowsAffected = cmd.ExecuteNonQuery();
}
}
catch (Exception ex) {
//Exception Handling
}
if (rowsAffected > 0)
{
//Successful
}
else {
//unsuccesfull
}
答案 1 :(得分:0)
调试代码。
打印异常消息。
检查受影响的行的值
检查xmlDataField列的数据类型和SqlParameter中的数据类型。
答案 2 :(得分:0)
enter code here
using (var client = new WebClient())
{
string xmlData = new System.Net.WebClient().DownloadString("http://timesofindia.indiatimes.com/rssfeedstopstories.cms");
XmlDocument xml = new XmlDocument();
xml.LoadXml(xmlData);
XmlNodeList nodeList = xml.SelectNodes("/rss/channel/item");
foreach (XmlNode node in nodeList)
{
var title = node["title"].InnerText;
var description = node["description"].InnerText;
var link = node["link"].InnerText;
var guid = node["guid"].InnerText;
var pubdate = node["pubDate"].InnerText;
SqlConnection cnn = new SqlConnection(@"Data Source=vishal-pc\mssqlserver2012;Initial Catalog=aditya;User ID=sa;password=1234");
SqlCommand cmd = new SqlCommand("insert into XMLdata (title,description,link,guid,pubdate) VALUES (@xmlData,@xmlData1,@xmlData2,@xmlData3,@xmlData4)", cnn);
//string sqlForInsert = "INSERT INTO XMLdata (title,description,link,guid,pubdate) VALUES (@xmlData,@xmlData1,@xmlData2,@xmlData3,@xmlData4);";
int rowsAffected = 0;
//try
//{
// using (SqlConnection con = new SqlConnection(@"Data Source=vishal-pc\mssqlserver2012;Initial Catalog=aditya;User ID=sa;password=1234"))
// using (SqlCommand cmd = new SqlCommand(sqlForInsert, con))
{
cmd.Parameters.Add(new SqlParameter("@xmlData", title));
cmd.Parameters.Add(new SqlParameter("@xmlData1", description));
cmd.Parameters.Add(new SqlParameter("@xmlData2", link));
cmd.Parameters.Add(new SqlParameter("@xmlData3", guid));
cmd.Parameters.Add(new SqlParameter("@xmlData4", pubdate));
cnn.Open();
rowsAffected = cmd.ExecuteNonQuery();
cnn.Close();
}
答案 3 :(得分:0)
XElement rssFeeds = null;
using (var client = new WebClient())
{
try
{
string rssString = new System.Net.WebClient().DownloadString("http://timesofindia.indiatimes.com/rssfeedstopstories.cms");
rssFeeds = XElement.Parse(rssString);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
if (rssFeeds != null)
{
using (SqlConnection con = new SqlConnection(@"Data Source=vishal-pc\mssqlserver2012;Initial Catalog=aditya;User ID=sa;password=1234"))
{
con.Open();
var tran = con.BeginTransaction();
try
{
rssFeeds.Descendants("item").ToList().ForEach(e =>
{
string insertSql = @"insert into XMLdata (title,description,link,guid,pubdate) VALUES (@title,@description,@link,@guid,pubdate)";
SqlCommand cmd = new SqlCommand(insertSql, con, tran);
cmd.Parameters.Add(new SqlParameter("title", e.Element("title").Value));
cmd.Parameters.Add(new SqlParameter("description", e.Element("description").Value));
cmd.Parameters.Add(new SqlParameter("link", e.Element("link").Value));
cmd.Parameters.Add(new SqlParameter("guid", e.Element("guid").Value));
cmd.Parameters.Add(new SqlParameter("pubdate", e.Element("pubDate").Value));
cmd.ExecuteNonQuery();
});
}
catch (Exception ex)
{
tran.Rollback();
Debug.WriteLine(ex.Message);
}
}
}