Click here to Skip to main content
16,008,954 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,I Am Beginner
Please help me to create an application that Read web page source code by using C#.NET And Find Custom text in the page Source And Create a text file.
Thanks.
---
C# 2010 or 2013

What I have tried:

very much search on many questions.
Posted
Updated 29-Aug-16 3:59am

1 solution

This can be done in many ways, this is one of them
Create a windows application and add webbrowser[^] control to it.
Load the url and query the element from the DOM to read the text and write it into a text file[^]

Simple example: it will save your name to the text file, by passing this question url to it.

C#
using System;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string url = "http://www.codeproject.com/Questions/1121032/Find-custom-textin-pagesource";
            WebBrowser web = new WebBrowser();
            web.Url = new Uri(url);
            web.DocumentCompleted += web_DocumentCompleted;
        }

        void web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser web = (sender as WebBrowser);
            string name = web.Document.GetElementById("ctl00_ctl00_MC_AMC_QuestionAuthorRepInfo_MemberName").InnerText;
            MessageBox.Show(name);

            string path = @"D\yourFile.txt";
            if (!File.Exists(path))
                using (StreamWriter sw = File.CreateText(path))
                    sw.WriteLine(name);
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900