c# - WPF Webbrowser create html file from user input -


i'm reading html using webbrowser component in wpf. should let user give there own text, upload image , save .html file.

by way can tag value:

 string mytagvalue = (framewithingrid.document mshtml.htmldocument)              .getelementsbytagname("div")              .oftype<mshtml.ihtmlelement>()              .first()              .innertext; 

how can get value text box , save .html file including new image?

need help. thank you.

this html file:

    <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html lang="ja"> <head> <meta http-equiv="content-type" content="text/html; charset=shift_jis"> <title>welcome linguini party!</title> <style type="text/css"> * {     margin: 0; }  html, body {     height: 100%;     font-family: arial;     background-color: #e1dfe3;     color: #333333;     background-repeat: no-repeat; }  h1 {     margin-right: 8%;     margin-left: 7%;     font-size: 450%;     text-align: center; } h2 {     font-size: 450%;     text-align: center;     color: #8a00ca;     font-style: italic; } </style> </head>  <body id="imagebg" background="welcome_final1.png"> <div id="container"> <h1 id="text1">welcome to<br></h1> <h2 id="text2">linguini party!</h2> </div> </body> </html> 

one strategy accomplishing create html template along lines of this...

<!doctype html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head>     <meta charset="utf-8" />     <title></title> </head> <body id="imagebg" >     <div id="container">         <h1 id="text1">$$text1$$<br></h1>         <h2 id="text2">$$text2$$</h2>         <img src="$$imagelocation$$"/>     </div> </body> </html> 

this template has been instrumented markers such $$text1$$. these replaced customized text later.

include template resource, or external file, or wherever. opt including embedded resource. here's settings...

enter image description here

you don't have use embedded resource, can use webbrowser html also. using resource because avoids exceptions caused missing or corrupt files.

the next step easy. here's working view model...

public class viewmodel {     public string text1 { get; set; }     public string text2 { get; set; }     public string imagepath { get; set; }     public string destinationname { get; set; }     public void start()     {         var resourcename = assembly.getexecutingassembly().getmanifestresourcenames().where(q => q.contains("template.html")).firstordefault();         using (stream stream = assembly.getexecutingassembly().getmanifestresourcestream(resourcename))         {             using (streamreader reader = new streamreader(stream))             {                 string html = reader.readtoend().replace("$$text1$$", text1).replace("$$text2$$", text2).replace("$$imagelocation$$", imagepath);                 file.writealltext(destinationname, html);             }         }     } } 

the view model gets various text1 (etc) properties set caller, , loads html template , replaces markers custom content. uses static method on file save it.

you can modify strategy use other type of instrumentation long it's consistent. more scalable, full-blown solution, suggest htmlagility. have been using years.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -