Pergunta

Eu preciso converter documentos HTML em XML válido, preferencialmente XHTML. Qual é a melhor forma de fazer isso? Alguém sabe um toolkit / library / sample / ... seja lá o que me ajuda a se essa tarefa feito?

Para ser um pouco mais claro aqui, meu aplicativo tem que fazer a conversão automaticamente em tempo de execução. Eu não olho para uma ferramenta que me ajuda a mover algumas páginas para XHTML manualmente.

Foi útil?

Solução

Converter de HTML para XML com HTML Tidy

download binários

JRoppert, para sua necessidade, eu acho que você pode querer olhar para o Fontes

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

Outras dicas

Você pode usar um href="http://www.codeplex.com/htmlagilitypack" rel="noreferrer"> HTML agilidade pack . Seu projeto open-source do CodePlex.

O Validator.nu HTML Parser vem com um programa de exemplo HTML2XML que faz a conversão usando o HTML5 análise de algoritmos e regras infoset coerção.

Use Html2Xhtml for .NET 4.0:

Em memória conversão de cadeia-to-string:

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

Em memória corda-a-XDocument conversão:

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

http://corsis.sourceforge.net/index.php/Html2Xhtml para mais informações.

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

Html2Xhtml é uma biblioteca .NET 4.0 para converter HTML para XHTML licenciado sob a GPLv2 ou acima.

Eu testei Html2Xhtml na reconstrução local de um grande banco de dados on-line da União Europeia. Tidy / Tidy.NET nem sequer produzir uma saída válida na maioria das vezes, de Chilkat HTML-to-XML foi um pouco lento e produziu resultados estranhos (extraviado, ausentes elementos inexplicáveis). Na tentativa de encontrar uma ferramenta de conversão livre, rápido e confiável eu criei esta biblioteca. Ele converte. 2 - 4x mais rápido do que todas as outras bibliotecas que eu testei

Html2Xhtml, combinado com o poder do LINQ to XML, é uma excelente ferramenta para todos os extração de dados em grande escala e web crawling cenários.

Você pode converter html para xhtml com arrumado arquivo executável:

arrumado -asxhtml -numeric index.xhml

você pode verificar o c # implementação aqui .

A maneira mais fácil é configurar o Visual Studio IDE para identificar as mudanças que você precisa fazer. Você pode fazer isso no Visual Studio 2008, vá para: Ferramentas, Opções, Texto Editor, HTML, validação e escolher o destino apropriado. Possivelmente XHTML 1.1 ou XHTML 1.0 Transitional.

Para alguma informação sobre os diferentes tipos, leia-se: http://msdn.microsoft.com/en-us/library/aa479043. aspx

Em seguida, você precisa trabalhar através dos pontos destacados em sua página.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top