Chevy Mark Sunderland

Chevy Mark Sunderland

  • 2k
  • 188
  • 160.1k

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
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();

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'));";

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;

xslt file

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