Who to write HTML code using Csharp
-
private void button1_Click(object sender, EventArgs e)
{
string[] input = new string[] { "Web", "Images", "Gmail", "C:\\Users\\Amit_Personal\\Documents\\Visual Studio 2008\\Projects\\abc\\abc\\google_image_final.png", "Google Search", "I'm Lucky"};// Pass the array as a parameter: Designpage(input); } public void Designpage(string\[\] arr) { string fileLoc = @"C:\\Users\\Amit\_Personal\\Documents\\Visual Studio 2008\\Projects\\abc\\abc\\a.htm"; if (File.Exists(fileLoc)) { using (StreamWriter s = File.AppendText(fileLoc)) { // StringWriter s = new StringWriter(); s.WriteLine("{0}", "<!DOCTYPE HTML PUBLIC \\"-//W3C//DTD HTML 4.0 Transitional//EN\\">"); s.WriteLine("{0}", "<html>"); s.WriteLine("{0}", "<title>Google</title>"); s.WriteLine("{0}", "<link rel=\\"stylesheet\\" type=\\"text/css\\" href=\\"style.css\\">"); s.WriteLine("{0}", "</head>"); s.WriteLine("{0}", "<body>"); s.WriteLine("<h4>{0} ", arr\[0\]); s.Write("{0} ", arr\[1\]); s.WriteLine("{0} </h4>", arr\[2\]); s.WriteLine("<img src=\\"{0}\\" />",arr\[3\]); s.WriteLine("{0}", "</body>"); s.WriteLine("{0}", "</html>"); s.WriteLine("{0}", ""); s.Close(); } }
Any one Know how to write better code to generate a .html file using C# language..
-
private void button1_Click(object sender, EventArgs e)
{
string[] input = new string[] { "Web", "Images", "Gmail", "C:\\Users\\Amit_Personal\\Documents\\Visual Studio 2008\\Projects\\abc\\abc\\google_image_final.png", "Google Search", "I'm Lucky"};// Pass the array as a parameter: Designpage(input); } public void Designpage(string\[\] arr) { string fileLoc = @"C:\\Users\\Amit\_Personal\\Documents\\Visual Studio 2008\\Projects\\abc\\abc\\a.htm"; if (File.Exists(fileLoc)) { using (StreamWriter s = File.AppendText(fileLoc)) { // StringWriter s = new StringWriter(); s.WriteLine("{0}", "<!DOCTYPE HTML PUBLIC \\"-//W3C//DTD HTML 4.0 Transitional//EN\\">"); s.WriteLine("{0}", "<html>"); s.WriteLine("{0}", "<title>Google</title>"); s.WriteLine("{0}", "<link rel=\\"stylesheet\\" type=\\"text/css\\" href=\\"style.css\\">"); s.WriteLine("{0}", "</head>"); s.WriteLine("{0}", "<body>"); s.WriteLine("<h4>{0} ", arr\[0\]); s.Write("{0} ", arr\[1\]); s.WriteLine("{0} </h4>", arr\[2\]); s.WriteLine("<img src=\\"{0}\\" />",arr\[3\]); s.WriteLine("{0}", "</body>"); s.WriteLine("{0}", "</html>"); s.WriteLine("{0}", ""); s.Close(); } }
Any one Know how to write better code to generate a .html file using C# language..
-
private void button1_Click(object sender, EventArgs e)
{
string[] input = new string[] { "Web", "Images", "Gmail", "C:\\Users\\Amit_Personal\\Documents\\Visual Studio 2008\\Projects\\abc\\abc\\google_image_final.png", "Google Search", "I'm Lucky"};// Pass the array as a parameter: Designpage(input); } public void Designpage(string\[\] arr) { string fileLoc = @"C:\\Users\\Amit\_Personal\\Documents\\Visual Studio 2008\\Projects\\abc\\abc\\a.htm"; if (File.Exists(fileLoc)) { using (StreamWriter s = File.AppendText(fileLoc)) { // StringWriter s = new StringWriter(); s.WriteLine("{0}", "<!DOCTYPE HTML PUBLIC \\"-//W3C//DTD HTML 4.0 Transitional//EN\\">"); s.WriteLine("{0}", "<html>"); s.WriteLine("{0}", "<title>Google</title>"); s.WriteLine("{0}", "<link rel=\\"stylesheet\\" type=\\"text/css\\" href=\\"style.css\\">"); s.WriteLine("{0}", "</head>"); s.WriteLine("{0}", "<body>"); s.WriteLine("<h4>{0} ", arr\[0\]); s.Write("{0} ", arr\[1\]); s.WriteLine("{0} </h4>", arr\[2\]); s.WriteLine("<img src=\\"{0}\\" />",arr\[3\]); s.WriteLine("{0}", "</body>"); s.WriteLine("{0}", "</html>"); s.WriteLine("{0}", ""); s.Close(); } }
Any one Know how to write better code to generate a .html file using C# language..
You could create a file that is the template and contains the content of the markup. for example:
blah blah blah...
{0}
{1}
{3}Then you can read in that file and use that in a single string.Format line to create the files from a template.
public void DesignPage(string TemplateFile, string OutputFile, string[] Contents){
/* skipping file checks, array checks, etc */
string content = File.ReadAllText(TemplateFile);
content = string.Format(content, Contents);
File.WriteAllText(OutputFile, content);
/* clean up, etc */
}You can also update the file templates without having to recompile, etc.
-
private void button1_Click(object sender, EventArgs e)
{
string[] input = new string[] { "Web", "Images", "Gmail", "C:\\Users\\Amit_Personal\\Documents\\Visual Studio 2008\\Projects\\abc\\abc\\google_image_final.png", "Google Search", "I'm Lucky"};// Pass the array as a parameter: Designpage(input); } public void Designpage(string\[\] arr) { string fileLoc = @"C:\\Users\\Amit\_Personal\\Documents\\Visual Studio 2008\\Projects\\abc\\abc\\a.htm"; if (File.Exists(fileLoc)) { using (StreamWriter s = File.AppendText(fileLoc)) { // StringWriter s = new StringWriter(); s.WriteLine("{0}", "<!DOCTYPE HTML PUBLIC \\"-//W3C//DTD HTML 4.0 Transitional//EN\\">"); s.WriteLine("{0}", "<html>"); s.WriteLine("{0}", "<title>Google</title>"); s.WriteLine("{0}", "<link rel=\\"stylesheet\\" type=\\"text/css\\" href=\\"style.css\\">"); s.WriteLine("{0}", "</head>"); s.WriteLine("{0}", "<body>"); s.WriteLine("<h4>{0} ", arr\[0\]); s.Write("{0} ", arr\[1\]); s.WriteLine("{0} </h4>", arr\[2\]); s.WriteLine("<img src=\\"{0}\\" />",arr\[3\]); s.WriteLine("{0}", "</body>"); s.WriteLine("{0}", "</html>"); s.WriteLine("{0}", ""); s.Close(); } }
Any one Know how to write better code to generate a .html file using C# language..
Here's one other way to do it... Not necessarily the best, but different, and prevents you from forgetting a closing tag.
XDocument doc = new XDocument(
new XDocumentType("HTML", "PUBLIC", "-//W3C//DTD HTML 4.0 Transitional//EN"),
new XElement("html",
new XElement("head",
new XElement("title", "Google"),
new XElement("link",
new XAttribute("rel", "stylesheet"),
new XAttribute("type", "text/css")
)
),
new XElement("body",
new XElement("h4", arr[0] + arr[1] + arr[2]),
new XElement("img",
new XAttribute("src", arr[3])
)
)
)
);Or something like that...
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
private void button1_Click(object sender, EventArgs e)
{
string[] input = new string[] { "Web", "Images", "Gmail", "C:\\Users\\Amit_Personal\\Documents\\Visual Studio 2008\\Projects\\abc\\abc\\google_image_final.png", "Google Search", "I'm Lucky"};// Pass the array as a parameter: Designpage(input); } public void Designpage(string\[\] arr) { string fileLoc = @"C:\\Users\\Amit\_Personal\\Documents\\Visual Studio 2008\\Projects\\abc\\abc\\a.htm"; if (File.Exists(fileLoc)) { using (StreamWriter s = File.AppendText(fileLoc)) { // StringWriter s = new StringWriter(); s.WriteLine("{0}", "<!DOCTYPE HTML PUBLIC \\"-//W3C//DTD HTML 4.0 Transitional//EN\\">"); s.WriteLine("{0}", "<html>"); s.WriteLine("{0}", "<title>Google</title>"); s.WriteLine("{0}", "<link rel=\\"stylesheet\\" type=\\"text/css\\" href=\\"style.css\\">"); s.WriteLine("{0}", "</head>"); s.WriteLine("{0}", "<body>"); s.WriteLine("<h4>{0} ", arr\[0\]); s.Write("{0} ", arr\[1\]); s.WriteLine("{0} </h4>", arr\[2\]); s.WriteLine("<img src=\\"{0}\\" />",arr\[3\]); s.WriteLine("{0}", "</body>"); s.WriteLine("{0}", "</html>"); s.WriteLine("{0}", ""); s.Close(); } }
Any one Know how to write better code to generate a .html file using C# language..
Similar to what Ian said, but using an XmlDocument: (Strictly off-the-cuff, not tested, however I did something like this last week to generate an HTML Table in a string)
XmlDocument doc = new XmlDocument() ;
XmlNode nod ;doc.AppendChild ( doc.CreateElement ( "html" ) ) ;
doc.DocumentElement.AppendChild ( nod = doc.CreateElement ( "head" ) ) ;
nod.AppendChild ( nod = doc.CreateElement ( "title" ) ) ;
nod.InnerText = "Google" ;doc.DocumentElement.AppendChild ( nod = doc.CreateElement ( "body" ) ) ;
...
doc.WriteTo ( somestream ) ; // To write to a stream
somestring = doc.OuterXml ; // To copy to a stringAlso,
amit_ghosh18 wrote:
s.WriteLine("{0}", "<html>");
you needn't use the "{0}" to write out a single string: s.WriteLine ( "html" ) ; s.WriteLine ( somestring ) ;
-
private void button1_Click(object sender, EventArgs e)
{
string[] input = new string[] { "Web", "Images", "Gmail", "C:\\Users\\Amit_Personal\\Documents\\Visual Studio 2008\\Projects\\abc\\abc\\google_image_final.png", "Google Search", "I'm Lucky"};// Pass the array as a parameter: Designpage(input); } public void Designpage(string\[\] arr) { string fileLoc = @"C:\\Users\\Amit\_Personal\\Documents\\Visual Studio 2008\\Projects\\abc\\abc\\a.htm"; if (File.Exists(fileLoc)) { using (StreamWriter s = File.AppendText(fileLoc)) { // StringWriter s = new StringWriter(); s.WriteLine("{0}", "<!DOCTYPE HTML PUBLIC \\"-//W3C//DTD HTML 4.0 Transitional//EN\\">"); s.WriteLine("{0}", "<html>"); s.WriteLine("{0}", "<title>Google</title>"); s.WriteLine("{0}", "<link rel=\\"stylesheet\\" type=\\"text/css\\" href=\\"style.css\\">"); s.WriteLine("{0}", "</head>"); s.WriteLine("{0}", "<body>"); s.WriteLine("<h4>{0} ", arr\[0\]); s.Write("{0} ", arr\[1\]); s.WriteLine("{0} </h4>", arr\[2\]); s.WriteLine("<img src=\\"{0}\\" />",arr\[3\]); s.WriteLine("{0}", "</body>"); s.WriteLine("{0}", "</html>"); s.WriteLine("{0}", ""); s.Close(); } }
Any one Know how to write better code to generate a .html file using C# language..
Why are you trying to make a fake Google site? For anything but the most trivial example I second the idea of using a template, and substituting special tokens for items in your data. In my HTTP server[^] the SubstitutingFileReader uses special tokens that look like HTML tags, which makes sense if the template is HTML (or another SGML language).