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

本文关键字:net asp SQL 数据 gt 何存储 存储 lt IEnumerable | 更新日期: 2023-09-27 18:20:34

我正在使用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提取值的样式。

请提出建议。

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

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

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中序列化对象并反序列化回对象的教程。

希望这对你有用。

干扰最小的方式:

var IcanbeQueried = myIEnumerable.AsQueryable();

然后,您可以保留现有的代码,只需将其转换为类似的IQueryable即可。