C# simple html editor - how to search and replace content - c#

Today i'm working on simple html editor in visual c#.
My goal is to open pure html file from local drive (Opendialog load into string or load into webbrowser completed) and allow to edit key fragments.
application should find specific divs and return the full content of that div to textbox or better to combobox (compare to combobox item and show it).
if i change the textbox (or pick up another item from combobox) application should present changes on webbrowser control.
Then i need to print this html as is seen on webbrowser control.
Last thing is to save modified hmtl overwrting original and adding comment with changes at bottom of the html file.
I want to know how to perform search&replace in this project? How to "adress" content of a div?
Better is string searching, indexof, string.replace etc. Or drown into DOM-thing (i don't know both at the moment).
How to present changes on html preview on webcontrol component? And finally overwrite a file?
Code's examples appreciated :)
Thanks in advance

You should use HtmlAgilityPack for that, it makes working with DOM a lot more easier, than parsing it yourself.
http://htmlagilitypack.codeplex.com/
For example, here is how to get all divs
var elements = hdoc.DocumentNode.Descendants("div") /.Where(.your conditions..))/;

Related

MS Office Word: How to create a custom minibar to a content control (richtext) in C#?

I am trying to modify a custom content control I've created. Specifically I want to add a minibar containing one or two buttons to a quick and short modification of the text included in my content control. The image below shows the outcome I want to have - the problem is that these buttons are only shown in built-in table content control and I can't find any useful information on the web about how to create something like that myself... Could you help me?
Unfortunately, the commands shown next to the content control title are not customizable; it's definitely something we've thought about, but this isn't possible today.
You could use the ContentControlOnEnter and ContentControlOnExit events to show buttons on the context menu or the ribbon; depending on your scenario, that might work?

WPF Template Form

I want to create a recipe application. I would like the input to look and feel like you are editing a document. If I were doing this in word, I'd create a template form for the user to use for the imput.
The form will look something like:
{Categoty} {Title}
{Image} {yield / nutrition info}
Ingredients
{bulleted list goes here}
Directions
{Numbered list goes here}
Notes / Comments
{Free form text goes here}
I tried doing this with a FlowDocument embedded im a RichTextBox, but could not figure it out. I can store the info and populate the FlowDocument parts easy enough, but I could not figure out how to control editing to force bullets or numbering at certain places / keeping the user from changing the format, etc.
Can this be done in a FlowDocument? If not, how can I create the bulleted / numbered list areas?
Flow Documents are editable as long as you use RichTextBox as opposed to Page.
you might want to take a look at this or this or even this
I ended up creating custom controls for the lists (custom grid supporting bulleted or numbered editable lists and using other controls for the various document parts to give me the control I want - I use an XML file for storing the pieces of the document and how to generate the FlowDocument (I hope this gives me the ease of updating the templates when I am asked to add something new)... I only generate a FlowDocument for printing purposes.
I did not get all the functionality I wanted, but I made it work. Now for my next project..

WebBrowser with limited tags support in C#

I need a editBox in c# that only shows and edits formatted texts(bolded,colored,...) and supports RTL and I can to get Html code from inputed text.
So used WebBrowser and enabled designMod on it. it's very good but it also shows unwanted contents (images,links,tables,...) if user pastes in it or drops to it.
I also used RichTextBox,But it have above problem and it don't provide html code for me.
If anyone can help me to implement a webbrowser control that cat not accepts some tags?
Sorry for bad English!!
Filter out the tags you don't want before you push it to your webbrowser. I don't believe there exists some premade control for this.

Integrating Visio exported XAML into Silverlight application

It is possible to export Microsoft Visio drawings as a Website containing Silverlight content. This is described on this blog-post.
The output of such an export are the following:
xaml_1.xaml (contains the structure of the control)
data.xml (contains all text content such as labels, etc)
several java-script files
*.htm pages with a Silverlight container
other files such as *.css and images
I would like to integrate the exported XAML code into another existing Silverlight application. I found this blog-post telling me how to load XAML code dynamically during runtime.
What I would like to know is how to "merge" the XAML-file and the data.xml and how I can get a reference to the items of the XAML code, in order to change certain texts...
In the associated xaml js file (eg xaml_1.js) there's a handleMouseUp function that reads the shape ID from the (XAML) 'name' string and then calls OnShapeClick in frameset.js. This method, which is common to all of the js-based Save as web output types, then calls other methods to populate the details table or retrieve hyperlinks found in data.xml. If you have a look at the FindShapeXML function in frameset.js you'll see that it gets the appropriate data based on the page and shape IDs (note that shape IDs are unique to a page as per Visio itself).
In terms of creating data-bound or dynamic shape text, one workaround for the glyphs issue that #slfan highlights is prevent the text from being output. For example, prior to running Save As Web in Visio, you could loop through all of the shapes and set their HideText ShapeSheet cell to true. This will prevent all of the glyphs xaml being generated and you'll still have access to the text string in data.xml. I guess you wouldn't then benefit from the correct font scaling, but it depends on your scenario. If it was really important to get the scale right then you could parse the RenderTransform attribute (which is described in attribute syntax rather than property element syntax) of the glyph elements.
Glyphs are there (I'm guessing) because it mirrors how Visio works in the application ie in Visio you can select individual characters within a shape's text and apply different fonts and formatting, but if you don't need that, I'd be tempted to ditch the glyphs collection and just use a TextBlock as #slfan suggests.
I think you have to tweak the generated XAML a little bit. Unfortunately Visio generates glyphs for every single character. If you want to change the text at runtime, you will have to remove this glyphs and add the required controls (e.g. TextBlock) yourself.
You can load the XAML into Silverlight with XamlReader.Load. A good description you find here: http://blogs.silverlight.net/blogs/msnow/archive/2008/10/09/silverlight-tip-of-the-day-60-how-to-load-a-control-straight-from-xaml.aspx.
All JavaScript and HTML files you can ignore, the XML-file you need to identify your controls. The ID's in the XML refer to the corresponding elements in the XAML-file.

How to create aspx page on runtime and place literal control?

I have an requirement where i have to display some content on iframe dynamically. Let me explain my scenario. I have a form where i have to display some html text. Earlier what i did is , i placed a literal on the form and fill it with HTML text. All worked fine but , the text alignments of the displayed html gets overriden by the page css.
So what i want now is that, instead of literal, i place an iframe. Now at page load event , i will generate a aspx dynamically and place a literal control and fill that literal control with the html text. And then will display that aspx page in the iframe.
So i would like to know the following things:
Is this a good idea to hold heavy
text , more than 20K of chars in
memory, dynamically generate aspx
page at runtime.
If any workaround to not to inherit
the page css inside the literal
displayed.
Any other approach better than my approach.
Way to implement my approach(iframe and dynamic aspx).
--- I am using ASP.NET / C# 4.0
Loads of questions...
Thanks in advance
AmRan, if this actually is a css problem, I would first try to create separate css styles for that content and use with the first solution you did. For example, wrap your dynamic content widh
<div id="dynamicContent">
tag and place
#dynamic {}
style settings inside your css file.
I don't think the Literal control has a CssClass property so you would have to use Label instead, or some other technique.
I would try to stay away from IFrame solutions if possible.
Hope this helps.

Categories

Resources