Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Who to write HTML code using Csharp

Who to write HTML code using Csharp

Scheduled Pinned Locked Moved C#
csharphtmlcssvisual-studiodata-structures
6 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    amit_ghosh18
    wrote on last edited by
    #1

    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}&nbsp;", arr\[0\]);
                       s.Write("{0}&nbsp;", arr\[1\]);
                       s.WriteLine("{0}&nbsp;</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..

    L M I P B 5 Replies Last reply
    0
    • A amit_ghosh18

      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}&nbsp;", arr\[0\]);
                         s.Write("{0}&nbsp;", arr\[1\]);
                         s.WriteLine("{0}&nbsp;</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..

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      using (StreamWriter sw = File.AppendText(fileLoc))
      {
      sw.WriteLine(string.Format(@"

      Google

      {0}
      {1}
      {2}

      ", arr[0], arr[1], arr[2], arr[3]);
      }

      Bastard Programmer from Hell :suss:

      1 Reply Last reply
      0
      • A amit_ghosh18

        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}&nbsp;", arr\[0\]);
                           s.Write("{0}&nbsp;", arr\[1\]);
                           s.WriteLine("{0}&nbsp;</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..

        M Offline
        M Offline
        Matt Meyer
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • A amit_ghosh18

          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}&nbsp;", arr\[0\]);
                             s.Write("{0}&nbsp;", arr\[1\]);
                             s.WriteLine("{0}&nbsp;</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..

          I Offline
          I Offline
          Ian Shlasko
          wrote on last edited by
          #4

          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)

          1 Reply Last reply
          0
          • A amit_ghosh18

            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}&nbsp;", arr\[0\]);
                               s.Write("{0}&nbsp;", arr\[1\]);
                               s.WriteLine("{0}&nbsp;</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..

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            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 string

            Also,

            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 ) ;

            1 Reply Last reply
            0
            • A amit_ghosh18

              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}&nbsp;", arr\[0\]);
                                 s.Write("{0}&nbsp;", arr\[1\]);
                                 s.WriteLine("{0}&nbsp;</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..

              B Offline
              B Offline
              BobJanova
              wrote on last edited by
              #6

              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).

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups