c#自动修复缺损和不规范的html

private string fixHTML(string html)
        {
            using (var reader = new SgmlReader())
            {
                reader.DocType = "HTML";
                reader.InputStream = new StringReader(html);

                using (var sw = new StringWriter())
                {
                    using (var writer = new XmlTextWriter(sw))
                    {
                        writer.Formatting = Formatting.Indented;
                        while (reader.Read())
                        {
                            if (reader.NodeType != XmlNodeType.Whitespace)
                            {
                                writer.WriteNode(reader, true);
                            }
                        }

                        var sb = new StringBuilder();
                        var doc = new XPathDocument(new StringReader(sw.ToString()));
                        var nav = doc.CreateNavigator();
                        var result = nav.SelectSingleNode("/").OuterXml;
                        return result;
                    }
                }
            }
        }

 

posted on 2021-02-19 14:43  空明流光  阅读(167)  评论(0编辑  收藏  举报

导航