Click here to Skip to main content
15,906,455 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
Hi,
I am creating the web application.
I need to find Source Code of a particular
in String format.
Is it Possible ? If yes than How????


Thanks in advance!!!!
Posted

You can try this code:

C#
private string readHtmlPage(string url)
      {
          string result;
          WebResponse objResponse;
          WebRequest objRequest = System.Net.HttpWebRequest.Create(url); // create a request
          objResponse = objRequest.GetResponse(); // get a response

          StreamReader sr = new StreamReader(objResponse.GetResponseStream()); // get the response stream

              result = sr.ReadToEnd(); // read the response stream
              // Close and clean up the StreamReader
              sr.Close();


          return result;
      }

Hope this helps.
 
Share this answer
 
Comments
Upniwesh 31-Aug-12 0:13am    
Thanks for help.
but i need to html code of only one Div. not the all page.
Like No. of 5 Div in my web page with different id. and i want the Code of only one Div for particular ID.
Thomas Daniels 31-Aug-12 2:54am    
At the top of your source code file, add 'using System.Xml'.
Then, use the next code:

XmlDocument xd = new XmlDocument();
xd.LoadXml(htmlSource);
XmlNodeList xmlNodeList = xd.GetElementsByTagName("div");
XmlNode divNode = null;
foreach (XmlNode xNode in xmlNodeList)
{
try
{
if (xNode.Attributes.GetNamedItem("id").Value == "div1")
{
divNode = xNode;
}
}
catch
{
// the node doesn't contain the attribute id
}
}
if (divNode != null)
{
MessageBox.Show(divNode.OuterXml); // shows the outer xml
MessageBox.Show(divNode.InnerXml); // shows the inner xml
MessageBox.Show(divNode.InnerText); // shows the inner text
}
else
{
MessageBox.Show("The specificed element is not found.");
}

Hope this helps.
If u want to display same code of application in browser
then u have to read source file from ur file location using filestream
 
Share this answer
 
Comments
Upniwesh 30-Aug-12 7:00am    
How to Get source file path???
C#
XmlDocument doc = new XmlDocument();

doc.Load("http://www.webpath.com");

string sours = doc.tostring();
 
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