将HTML解析为String c#时出现Null异常 [英] Null Exception when parsing HTML to String c#

查看:117
本文介绍了将HTML解析为String c#时出现Null异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将stringbuilder对象中构建的Html字符串解析为列表时,我继续获取null异常。如果我将字符串构建器对象替换为某些硬编码的HTML,它就会毫无障碍地消失。我知道字符串构建器实际上是在做它的工作,因为我将它输出到文本框。它显示完美的HTML,但是当我尝试使用HtmlWorker类调用该字符串时。有没有人知道为什么当它被引用为字符串时它会生成输出,但是当我尝试将它放在列表对象中时却没有?我已经包含了我的代码,我会粗略确定我收到错误的行。



  private   void  createPDF()
{
openFileDialog.Filter = HTML文件(.html)| * .html;
openFileDialog.FilterIndex = 0 ;
if (openFileDialog.ShowDialog()== DialogResult.OK)
{
System.Text.StringBuilder store = new System.Text.StringBuilder();
string fileName = openFileDialog.FileName;

尝试
{
使用(系统。 IO.StreamReader htmlReader = new System.IO.StreamReader(openFileDialog.FileName))
{
string 行;
while ((line = htmlReader.ReadLine())!= null
{
store.Append(line + Environment.NewLine);
}
this .textBox1.Text = store.ToString();
string html = store.ToString();
凭证凭证= 凭证(PageSize.LETTER, 20 25 35 35 );


PdfWriter.GetInstance(document, new FileStream( c:\\Data / my.pdf,FileMode.Create));
document.Open();
System.Collections.Generic.List< IElement> htmlarraylist = new 列表< IElement>(HTMLWorker.ParseToList( new StringReader(html), new StyleSheet()));
foreach (IElement element in htmlarraylist)
{
document.Add(element);
}
document.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString( ));
}





就像我说的,如果我换掉将Stringbuilder对象链接到字符串'html'的行工作良好。每次在粗线上都会出现Null引用。

解决方案

很高兴你得到它的工作。这里的解决方案是让问题从未答复的列表中删除。



问题是由于解析器无法处理的HTML标记而引发的空引用异常。解决方法是删除标签。


是的,它完成了。HTML渲染存在问题,因为在开发人员追加过程中没有正确制作一些HTML标签因此StringReader无法正确渲染。 / BLOCKQUOTE>

I keep on getting a null exception when I attempt to parse an Html string built in a stringbuilder object to an list. If I swap out the string builder object for some hard-coded HTML, it goes off without a hitch. I know the string builder is actually doing it's job because I have it outputting to a textbox. It displays flawless HTML, but when I attempt to call that string out with the HtmlWorker class. Does anyone have any clue why it would generate output when it is referenced as a string, but not when I attempt to drop it in the list object? I've included my code, and I will bold the exact Line I receive the error.

private void createPDF()
{
    openFileDialog.Filter = "HTML Files (.html)|*.html";
    openFileDialog.FilterIndex = 0;
    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        System.Text.StringBuilder store = new System.Text.StringBuilder();
        string fileName = openFileDialog.FileName;

        try
        {
            using (System.IO.StreamReader htmlReader = new System.IO.StreamReader(openFileDialog.FileName))
            {
                string line;
                while ((line = htmlReader.ReadLine()) != null)
                {
                    store.Append(line + Environment.NewLine);
                }
                this.textBox1.Text = store.ToString();
                string html = store.ToString();
                Document document = new Document(PageSize.LETTER, 20, 25, 35, 35);


                PdfWriter.GetInstance(document, new FileStream("c:\\Data/my.pdf", FileMode.Create));
                document.Open();
                System.Collections.Generic.List<IElement> htmlarraylist = new List<IElement>(HTMLWorker.ParseToList(new StringReader(html), new StyleSheet()));
                foreach (IElement element in htmlarraylist)
                {
                    document.Add(element);
                }
                document.Close();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }



Like I said, if I swap out the line that links the Stringbuilder object to the string 'html' it works fine. The Null reference occurs every time on the bold line.

解决方案

Glad you got it working. Solution here is to make the question drop off the unanswered list.

The problem was a null reference exception being thrown due to HTML tags that the parser was unable to handle. Solution was to remove the tags.


Yes,it done.There is problem in HTML rendering beacuse it some HTML tag not properly made during append by developer So that StringReader is not render properly .


这篇关于将HTML解析为String c#时出现Null异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆