【发布时间】:2014-04-04 11:28:01
【问题描述】:
我需要在 DB 中插入 xslt 文件的值,我尝试了这段代码。我在调试中没有错误,但没有执行插入。
非常感谢您在解决这个问题时给我的任何帮助,谢谢。
代码:
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 title = node.SelectSingleNode("title").InnerText;
string sql = "insert into Product values(" + title.ToString() + ")";
command = new OdbcCommand(sql, connection);
adpter.InsertCommand = command;
adpter.InsertCommand.ExecuteNonQuery();
}
XSLT 文件:
<?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>
SQL 注入的新版本,但在 DB 中仅注册 XSLT 文件的标题:
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();
}
这是浏览器上的 RSS 文件(查看源代码)。
我需要在数据库中插入值:
- 标题:新闻、ASP.NET 和 Web 开发和焦点;
- 用于描述:新闻、ASP.NET 和 Web 开发和焦点;
- 发布日期:格林威治标准时间 2014 年 4 月 4 日星期五 13:57:16、格林威治标准时间 2014 年 2 月 26 日星期三 09:39:00 和格林威治标准时间 2014 年 2 月 26 日星期三 09:39:00
我的代码现在只插入数据库:
- 标题:新闻;
- 用于描述:新闻;
-
发布日期:格林威治标准时间 2014 年 4 月 4 日星期五 13:57:16。
<title>News</title> <link>http://.... <description>News</description> <pubDate>Fri, 04 Apr 2014 13:57:16 GMT</pubDate> <dc:date>2014-04-04T13:57:16Z</dc:date> <image> <title>News</title> <url>http://....</url> <link>http://.... </image> <item> <title>ASP.NET & Web Development</title> <link>http://..... <description>ASP.NET & Web Development .....</description> <category> </category> <pubDate>Wed, 26 Feb 2014 09:39:00 GMT</pubDate> <guid isPermaLink="false">http://.....</guid> <dc:title>ASP.NET & Web Development</dc:title> <dc:creator> </dc:creator> <dc:description>ASP.NET & Web Development</dc:description> <dc:date>2014-02-26T09:39:00Z</dc:date> <dc:type>eip_news</dc:type> <dc:source>ASP.NET & Web Development</dc:source> <dc:language>us_US</dc:language> </item> <item> <title>In Focus</title> <link>http://..... <description>In Focus ..... </description> <category> </category> <pubDate>Wed, 26 Feb 2014 09:39:00 GMT</pubDate> <guid isPermaLink="false">http://.....</guid> <dc:title>In Focus</dc:title> <dc:creator> </dc:creator> <dc:description>In Focus</dc:description> <dc:date>2014-02-26T09:39:00Z</dc:date> <dc:type>eip_news</dc:type> <dc:source>In Focus</dc:source> <dc:language>us_US</dc:language> </item>
【问题讨论】:
-
据我所知,XSLT 代表 XSL 转换。您可以使用它将一个 XML 文档转换为另一种文档类型(不仅仅是 XML)。根据定义,它本身没有任何数据。那你想在数据库中存储什么?