Chevy Mark Sunderland

Chevy Mark Sunderland

  • NA
  • 188
  • 166.7k

C# net 4: How to insert data XML to database

Apr 4 2014 9:03 AM
I need import in my DB all news of this example page RSS http://www.SiteExample.com/News.rss
With my C# code page I show the RSS news in the browser and with this code the insert in DB is only for the general title of the news and not for single title, description and pubdate of single news.
I would greatly appreciate any help you can give me in working this problem, thank you.
Code behind:
Stream stream = resp.GetResponseStream();

XmlTextReader reader = new XmlTextReader(stream);
reader.XmlResolver = null;

XmlDocument doc = new XmlDocument();
doc.Load(reader);

xmlRSS.Document = doc;

XmlNodeList dataNodes = doc.SelectNodes("/title");

OdbcCommand command;
OdbcDataAdapter adpter = new OdbcDataAdapter();


foreach (XmlNode node in dataNodes)
{
string titlenew = node.SelectSingleNode("//title").InnerText;
string descriptionnew = node.SelectSingleNode("//description").InnerText;
string pubDatenew = node.SelectSingleNode("//pubDate").InnerText;
string sql = "insert into Product (title, description, pubdate) values (?,?, " +
" STR_TO_DATE(?, '%a, %d %b %Y %H:%i:%s GMT'));";

connection.Open();
command = new OdbcCommand(sql, connection);
command.Parameters.AddWithValue("param1", titlenew.ToString());
command.Parameters.AddWithValue("param2", descriptionnew.ToString());
command.Parameters.AddWithValue("param3", pubDatenew.ToString());
adpter.InsertCommand = command;
adpter.InsertCommand.ExecuteNonQuery();
connection.Close();
}

xslt file

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:param name="title"/>
<xsl:template match="rss">
<xsl:for-each select="channel/item">
<br>
<strong>
<a href="{link}" target="_main">
<xsl:value-of select="title"/>
</a>
</strong>
<br></br>
<xsl:value-of select="description" disable-output-escaping="yes"/>
</br>
<br></br>
<xsl:value-of select="pubDate"/>
<br></br>
</xsl:for-each>
</xsl:template>
<xsl:template match="description">
<br>
<xsl:value-of select="."/>
</br>
</xsl:template>
</xsl:stylesheet>