2016-07-21 11 views
0

XMLデータを文字列に読み込む方法を知っていますが、XMLファイルのノードにあるコンテンツをデータベースに格納できません。私はすでに多くの解決策を試してきましたが、私の場合は何も働いていません。 XMLリンクは次のとおりです。 -URLからXmLデータをデータベースに保存する方法

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

私はXMLファイルとXMLファイルのノードに存在するデータを保存したいURLからです。使用

(VARクライアント=新しいWebクライアント()){ ストリングです。xmlData =新しい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
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(); 
        } 
+0

唯一の変更は、xml.LoadXml(文字列)が存在する必要があります。これは必要な仕事をします –

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 
} 
を追加するには、需要量としてINSERTクエリ
    1. 変更接続文字列
    2. 変更を
  • +0

    は、まだデータがデータベースに保存されて取得されていない問題が存在し、uは、問題が何であるかを –

    +0

    out.thanks私を助けてeven.canエラーはありません、誰もが –

    +0

    してください、あなたを助けることができることを説明しますあなたの接続が正常に終了していることを確認してください。それ以外の場合は接続文字列をチェックしてください。 –

    0

    コードをデバッグします。
    例外メッセージを出力します。
    rowsAffectedの値を確認してください。
    xmlDataField列のデータ型とSqlParameterの列データ型を確認します。

    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); 
           } 
    
          } 
         } 
    
    関連する問題