3

I'm wondering if it possible to open a HTML Document to edit elements and attributes without the use of the WebBrowser class; I know how to use functions in HTML to do this, but unfortunately, I need the editing to occur as part of an add-in for another program (solidworks Enterprise PDM) which only allows the use of C# with NET 3.5.

I was thinking of using something like:

FileStream UpdateHTML = new FileStream(filepath, FileMode.Open, FileAccess.Write);

to give write access to html document, but I'm not sure if this is the correct path to be taking.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Louis
  • 35
  • 1
  • 3
  • Possible duplicate of [Looking for C# HTML Parser](http://stackoverflow.com/questions/100358/looking-for-c-html-parser) – Chris Shouts Jul 13 '11 at 18:48
  • Loading file, parsing, and saving back to file should work fine. See here for HTML parsing with .net: http://stackoverflow.com/questions/56107/what-is-the-best-way-to-parse-html-in-c – NoBugs Jul 13 '11 at 18:48

2 Answers2

6

You may take a look at Html Agility Pack which allows you to parse and manipulate HTML:

This is an agile HTML parser that builds a read/write DOM and supports plain XPATH or XSLT (you actually don't HAVE to understand XPATH nor XSLT to use it, don't worry...). It is a .NET code library that allows you to parse "out of the web" HTML files. The parser is very tolerant with "real world" malformed HTML. The object model is very similar to what proposes System.Xml, but for HTML documents (or streams).

carla
  • 1,970
  • 1
  • 31
  • 44
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

If it's XHTML you could very well use XmlDocument. Use its Load(string) method to load the file and you are good to go. To save your changes to the file just use the Save(string) method. Other than that, XmlDocument provides an API similar to the DOM.

XmlElement @ MSDN

  • Ill give both a try; currently I;m having to create an xml file using C#, and then access it with a script written in a separate HTML file. It tends to get a little awkward after a while. – Louis Jul 15 '11 at 00:31
  • sorry for the late replay. I tried the XMLDocument method first, and it appears to be the most successful, although formatting proved to be more difficult; the HTML agility pack did run quicker when creating an html document, but as the program needs to run off other workstations, the .NET files would need to also be moved onto the other computers. Thanks for you suggestions, definitely helped to point me in the right direction. – Louis Jul 21 '11 at 18:18