0

HTML ファイルをインデントする方法を見つけようとしています。XMLDocument を使用しており、XmlTextWriter を使用しています。

ただし、doctype をチェックしてダウンロードしようとするため、HTML ドキュメント用に正しくフォーマットできません。

ドキュメントを検証またはチェックせず、ベスト エフォート型のインデントを行う "ダム" インデント メカニズムはありますか? ファイルのサイズは 4 ~ 10Mb で、自動生成されます。内部で処理する必要があります。問題ありません。ユーザーは待つことができます。新しいプロセスへの分岐を避けたいだけです。

参照用の私のコードは次のとおりです

        using (MemoryStream ms = new MemoryStream())
        using (XmlTextWriter xtw = new XmlTextWriter(ms, Encoding.Unicode))
        {
            XmlDocument doc = new XmlDocument();
            // LoadSettings the unformatted XML text string into an instance
            // of the XML Document Object Model (DOM)
            doc.LoadXml(content);

            // Set the formatting property of the XML Text Writer to indented
            // the text writer is where the indenting will be performed
            xtw.Formatting = Formatting.Indented;

            // write dom xml to the xmltextwriter
            doc.WriteContentTo(xtw);

            // Flush the contents of the text writer
            // to the memory stream, which is simply a memory file
            xtw.Flush();

            // set to start of the memory stream (file)
            ms.Seek(0, SeekOrigin.Begin);

            // create a reader to read the contents of
            // the memory stream (file)
            using (StreamReader sr = new StreamReader(ms))
                return sr.ReadToEnd();
        }

基本的に、今は MemoryStream、XmlTextWriter、および XmlDocument を使用しています。一度インデントすると、MemoryStream から読み返し、文字列として返します。dtds を取得しようとするため、XHTML ドキュメントと一部の HTML 4 ドキュメントでエラーが発生します。XmlResolver を null として設定しようとしましたが、役に立ちませんでした:(

4

1 に答える 1

0

問題の原因となっている特定の X[H]TML にアクセスできない場合、これが機能するかどうかを判断するのは困難ですが、XDocument代わりに使用してみましたか?

XDocument xdoc = XDocument.Parse(xml);
string formatted = xdoc.ToString();
于 2010-04-30T01:50:43.317 に答える