【问题标题】:how can i save XmL data from a URL into my database如何将 URL 中的 XmL 数据保存到我的数据库中
【发布时间】:2016-11-24 21:48:54
【问题描述】:

我知道如何将 XML 数据加载到字符串中,但我无法将其内容(XML 文件节点中的内容)存储到我的数据库中。我已经尝试了很多解决方案,但没有一个对我有用。 XML 链接是:-

http://timesofindia.indiatimes.com/rssfeedstopstories.cms

我只想保存 XML 文件的节点中存在的数据,而 XML 文件来自 URL。

使用 (var client = new WebClient()) { 字符串 xmlData = new System.Net.WebClient().DownloadString("http://timesofindia.indiatimes.com/rssfeedstopstories.cms"); XmlDocument xml = 新的 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();
                    }

【问题讨论】:

    标签: c# sql-server-2012


    【解决方案1】:

    调试您的代码。
    打印异常消息。
    检查 rowsAffected 的值。
    检查xmlDataField列的数据类型和SqlParameter中的数据类型。

    【讨论】:

      【解决方案2】:

      试试这个所有你需要做的:

      1. 更改连接字符串
      2. 根据您的要求更改插入查询
      3. 添加其他参数

        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
        }
        

      【讨论】:

      • 仍然存在数据没有保存在数据库中的问题,甚至没有错误。你能帮帮我吗。谢谢
      • 有什么问题解释一下,任何人都可以帮你解决
      • 请确保您的连接已成功打开和关闭。否则检查你的连接字符串可能是主要问题
      • 使用这样的连接字符串@"Server=(local)\SQLEXPRESS;Database=databasename;User Id=sa;Password=password"
      • 即使我已经很好地打开和关闭了连接,连接字符串的格式也是正确的,但我无法判断它有什么问题以及为什么该数据没有保存在数据库中。请帮助。并且再告诉我一件事,在 catch 和 IF 和 ELSE 下写什么。
      【解决方案3】:
      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();
                          }
      

      【讨论】:

      • 唯一的变化是应该存在一个 xml.LoadXml(string);这将完成所需的工作
      【解决方案4】:
              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);
                      }
      
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-25
        • 1970-01-01
        • 2017-09-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多