【问题标题】:How to convert HTML to XHTML? [closed]如何将 HTML 转换为 XHTML? [关闭]
【发布时间】:2023-12-20 15:14:01
【问题描述】:

我需要将 HTML 文档转换为有效的 XML,最好是 XHTML。最好的方法是什么?有没有人知道工具包/库/示例/...任何可以帮助我完成任务的东西?

在这里更清楚一点,我的应用程序必须在运行时自动进行转换。我不寻找可以帮助我手动将某些页面移动到 XHTML 的工具。

【问题讨论】:

    标签: c# .net html xhtml converter


    【解决方案1】:

    Convert from HTML to XML with HTML Tidy

    Downloadable Binaries

    JRoppert,根据您的需要,我想您可能想查看Sources

    c:\temp>tidy -help
    tidy [option...] [file...] [option...] [file...]
    Utility to clean up and pretty print HTML/XHTML/XML
    see http://tidy.sourceforge.net/
    
    Options for HTML Tidy for Windows released on 14 February 2006:
    
    File manipulation
    -----------------
     -output <file>, -o  write output to the specified <file>
     <file>
     -config <file>      set configuration options from the specified <file>
     -file <file>, -f    write errors to the specified <file>
     <file>
     -modify, -m         modify the original input files
    
    Processing directives
    ---------------------
     -indent, -i         indent element content
     -wrap <column>, -w  wrap text at the specified <column>. 0 is assumed if
     <column>            <column> is missing. When this option is omitted, the
                         default of the configuration option "wrap" applies.
     -upper, -u          force tags to upper case
     -clean, -c          replace FONT, NOBR and CENTER tags by CSS
     -bare, -b           strip out smart quotes and em dashes, etc.
     -numeric, -n        output numeric rather than named entities
     -errors, -e         only show errors
     -quiet, -q          suppress nonessential output
     -omit               omit optional end tags
     -xml                specify the input is well formed XML
     -asxml, -asxhtml    convert HTML to well formed XHTML
     -ashtml             force XHTML to well formed HTML
     -access <level>     do additional accessibility checks (<level> = 0, 1, 2, 3).
                         0 is assumed if <level> is missing.
    
    Character encodings
    -------------------
     -raw                output values above 127 without conversion to entities
     -ascii              use ISO-8859-1 for input, US-ASCII for output
     -latin0             use ISO-8859-15 for input, US-ASCII for output
     -latin1             use ISO-8859-1 for both input and output
     -iso2022            use ISO-2022 for both input and output
     -utf8               use UTF-8 for both input and output
     -mac                use MacRoman for input, US-ASCII for output
     -win1252            use Windows-1252 for input, US-ASCII for output
     -ibm858             use IBM-858 (CP850+Euro) for input, US-ASCII for output
     -utf16le            use UTF-16LE for both input and output
     -utf16be            use UTF-16BE for both input and output
     -utf16              use UTF-16 for both input and output
     -big5               use Big5 for both input and output
     -shiftjis           use Shift_JIS for both input and output
     -language <lang>    set the two-letter language code <lang> (for future use)
    
    Miscellaneous
    -------------
     -version, -v        show the version of Tidy
     -help, -h, -?       list the command line options
     -xml-help           list the command line options in XML format
     -help-config        list all configuration options
     -xml-config         list all configuration options in XML format
     -show-config        list the current configuration settings
    
    Use --blah blarg for any configuration option "blah" with argument "blarg"
    
    Input/Output default to stdin/stdout respectively
    Single letter options apart from -f may be combined
    as in:  tidy -f errs.txt -imu foo.html
    For further info on HTML see http://www.w3.org/MarkUp
    

    【讨论】:

    • 不幸的是,第一个链接现在已失效。但似乎仍然可以在这里找到内容:ibm.com/developerworks/library/x-tiptidy/index.html
    • 要实际使用 tidy 创建 xhtml,正如人们所追求的,看起来你必须传递 -asxml 标志,否则你会得到非验证输出。
    【解决方案2】:

    您可以使用HTML Agility Pack。它来自 CodePlex 的开源项目。

    【讨论】:

      【解决方案3】:

      Validator.nu HTML Parser 带有一个 HTML2XML 示例程序,它使用 HTML5 解析算法和信息集强制规则进行转换。

      【讨论】:

        【解决方案4】:

        为 .NET 4.0 使用 Html2Xhtml:

        内存中字符串到字符串的转换:

        var xhtml = Html2Xhtml.RunAsFilter(stdin => stdin.Write(html)).ReadToEnd();
        

        内存中字符串到XDocument的转换:

        var xdoc = Html2Xhtml.RunAsFilter(stdin => stdin.Write(html)).ReadToXDocument();
        

        更多信息请参见http://corsis.sourceforge.net/index.php/Html2Xhtml

        【讨论】:

        • 我有完全相同的问题并使用了这个答案,效果很好。尤其是对 XElement 的转换。
        【解决方案5】:

        http://corsis.sourceforge.net/index.php/Html2Xhtmlhttp://corsis.sourceforge.net/index.php/Html2Xhtml

        Html2Xhtml 是一个 .NET 4.0 库,用于将 HTML 转换为 GPLv2 或更高版本许可的 XHTML。

        我在欧盟的一个大型在线数据库本地重构中测试了Html2Xhtml。 Tidy/Tidy.NET 在大多数情况下甚至都不会产生有效的输出,Chilkat 的 HTML 到 XML 有点慢并且产生了奇怪的结果(错位、丢失、无法解释的元素)。为了找到一个免费、快速和可靠的转换工具,我创建了这个库。它的转换速度比我测试的所有其他库快 2 到 4 倍。

        Html2Xhtml,结合 LINQ to XML 的强大功能,是所有大规模数据提取和网络爬取场景的绝佳工具。

        【讨论】:

          【解决方案6】:

          您可以使用整洁的可执行文件将 html 转换为 xhtml:

          tidy -asxhtml -numeric index.xhml

          你可以查看c#实现here

          【讨论】:

            【解决方案7】:

            最简单的方法是设置您的 Visual Studio IDE 以识别您需要进行的更改。 您可以在 Visual Studio 2008 中执行此操作,方法是: 工具、选项、文本编辑器、HTML、验证并选择适当的目标。 可能是 XHTML 1.1 或 XHTML 1.0 过渡。

            有关不同类型的一些信息,请阅读: http://msdn.microsoft.com/en-us/library/aa479043.aspx

            然后您需要处理页面上突出显示的点。

            【讨论】:

            • 对不起,我的问题不清楚。我需要在运行时自动进行转换。