如何存储 IEnumerable<> asp.net 中 SQL 表中的数据值?

发布于 2024-12-27 18:23:17 字数 1323 浏览 0 评论 0 原文

我正在使用 HtmlAgilityPack 从网页检索信息,目前,我正在使用它在按钮单击方法中使用 ListView 控件在页面本身上显示值。

 protected void Button1_Click(object sender, EventArgs e)
 {
     string url = TextBox1.Text.ToString();
     var webGet = new HtmlWeb();
     var document = webGet.Load(url);
     // Below code crawl the data and store in generic IEnumerable<T> fashion //
     var TheWeb = 
           from info in document.DocumentNode.SelectNodes("//div[@class='article-listing']//div[@class='media-data']")
           from link in info.SelectNodes("h4//a").Where(x => x.Attributes.Contains("href"))
           from content in info.SelectNodes("p").Where(y => y.HasAttributes != true)
           from author in info.SelectNodes("p[@class='article-meta']//a[@rel='author']").Where(z => z.Attributes.Contains("href"))
           from date in info.SelectNodes("p[@class='article-meta']//span")
           select new
           {
                 LinkURL = link.Attributes["href"].Value,
                 Text = content.InnerText,
                 Author = author.InnerText,
                 Date = date.InnerText
           };
     lvLinks.DataSource = TheWeb;
     lvLinks.DataBind(); 
}

但现在我想将数据存储在 SQL Server 中,并想使用某些函数(而不是单击按钮)运行代码。

为此,我想以其他形式存储数据,而不是 IEnumerable<>使用 LINQ 提取值的样式。

请建议。

I am using the HtmlAgilityPack to retrieve information from a web page and, at present, am using it to show values on the page itself using a ListView control in a button click method.

 protected void Button1_Click(object sender, EventArgs e)
 {
     string url = TextBox1.Text.ToString();
     var webGet = new HtmlWeb();
     var document = webGet.Load(url);
     // Below code crawl the data and store in generic IEnumerable<T> fashion //
     var TheWeb = 
           from info in document.DocumentNode.SelectNodes("//div[@class='article-listing']//div[@class='media-data']")
           from link in info.SelectNodes("h4//a").Where(x => x.Attributes.Contains("href"))
           from content in info.SelectNodes("p").Where(y => y.HasAttributes != true)
           from author in info.SelectNodes("p[@class='article-meta']//a[@rel='author']").Where(z => z.Attributes.Contains("href"))
           from date in info.SelectNodes("p[@class='article-meta']//span")
           select new
           {
                 LinkURL = link.Attributes["href"].Value,
                 Text = content.InnerText,
                 Author = author.InnerText,
                 Date = date.InnerText
           };
     lvLinks.DataSource = TheWeb;
     lvLinks.DataBind(); 
}

But now I want to store the data in SQL Server and want to run the code using some function (instead of button click).

For that I want to store the data in some other form instead of an IEnumerable<> style which uses LINQ to extract values.

Please suggest.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无风消散 2025-01-03 18:23:17

您可以拥有一个具有以下结构的自定义类:

public class ParseData
{
    public string LinkURL { get; set; }
    public string Text { get; set; } 
    public string Author { get; set; }
    public string Date { get; set; }
}

使用您的查询填充它

var TheWeb = 
       from info in document.DocumentNode.SelectNodes("//div[@class='article-listing']//div[@class='media-data']")
       from link in info.SelectNodes("h4//a").Where(x => x.Attributes.Contains("href"))
       from content in info.SelectNodes("p").Where(y => y.HasAttributes != true)
       from author in info.SelectNodes("p[@class='article-meta']//a[@rel='author']").Where(z => z.Attributes.Contains("href"))
       from date in info.SelectNodes("p[@class='article-meta']//span")
       select new ParseData
       {
             LinkURL = link.Attributes["href"].Value,
             Text = content.InnerText,
             Author = author.InnerText,
             Date = date.InnerText
       };
var parseData = TheWeb.ToList();

,现在使用 System.Xml.Serialization.XmlSerializer 将此数据序列化为 XML。将此 XML 存储在数据库中并在需要时检索。

说明如何序列化 XML 中的对象以及反序列化回对象的教程

希望这对你有用。

You can have a custom class with the structure

public class ParseData
{
    public string LinkURL { get; set; }
    public string Text { get; set; } 
    public string Author { get; set; }
    public string Date { get; set; }
}

Populate it with your query

var TheWeb = 
       from info in document.DocumentNode.SelectNodes("//div[@class='article-listing']//div[@class='media-data']")
       from link in info.SelectNodes("h4//a").Where(x => x.Attributes.Contains("href"))
       from content in info.SelectNodes("p").Where(y => y.HasAttributes != true)
       from author in info.SelectNodes("p[@class='article-meta']//a[@rel='author']").Where(z => z.Attributes.Contains("href"))
       from date in info.SelectNodes("p[@class='article-meta']//span")
       select new ParseData
       {
             LinkURL = link.Attributes["href"].Value,
             Text = content.InnerText,
             Author = author.InnerText,
             Date = date.InnerText
       };
var parseData = TheWeb.ToList();

And now using System.Xml.Serialization.XmlSerializer serialize this data in XML. Store this XML in your DB and retrieve when ever required.

Tutorial explaining how to Serialize object in XML and Deserialize back to object.

Hope this works for you.

请止步禁区 2025-01-03 18:23:17

最少干扰的方式:

var IcanbeQueried = myIEnumerable.AsQueryable();

然后你可以保留你拥有的代码,你只需要将它转换为 IQueryable 就可以了。

Least interfering way:

var IcanbeQueried = myIEnumerable.AsQueryable();

Then you can keep the code you have, you only have to convert it to an IQueryable like so.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文