gpt4 book ai didi

c# - C# 中带有 HtmlDocument 引用的 NullReferenceException

转载 作者:太空宇宙 更新时间:2023-11-03 11:14:39 25 4
gpt4 key购买 nike

我正在使用 HtmlAgilityPack 来从谷歌翻译中抓取翻译程序的信息。我已经下载了 HtmlAgilityPack dll,并在我的程序中成功引用了它。我在 Unity 中使用 Assembly。下面是我为这两个程序编写的代码:

using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using HtmlAgilityPack;

public class GUIScript : MonoBehaviour {
private string textField = "";
private string input;
public Texture2D icon;
Dictionary search;
Encoding code;
// Use this for initialization
void Start () {
search = new Dictionary();
input = " ";
code = Encoding.UTF8;
//This is what is run to translate
print (search.Translate("Hola","es|en",code));
}

// Update is called once per frame
void Update () {

}
void OnGUI(){
textField = GUI.TextField(new Rect(0, Screen.height -50, Screen.width-80, 40), textField);
if(GUI.Button(new Rect(Screen.width-80, Screen.height -50, 80,40), icon)){
input = textField;
textField = "";

}
//GUI.Label(new Rect(0,Screen.height -70, Screen.width-80,20), search.Translate("Hola","es|en",code));
//print (search.Translate("Hola","es|en",code));
}
}

这是引用我的 Dictionary 类的代码,如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using System.Collections;
using System.Net;
using HtmlAgilityPack;


public class Dictionary{
string[] formatParams;
HtmlDocument doc;
public Dictionary(){
formatParams = new string[2];
doc = new HtmlDocument();
}
public string Translate(String input, String languagePair, Encoding encoding)
{
formatParams[0]= input;
formatParams[1]= languagePair;
string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", formatParams);

string result = String.Empty;

using (WebClient webClient = new WebClient())
{
webClient.Encoding = encoding;
result = webClient.DownloadString(url);
}
doc.LoadHtml(result);
return doc.DocumentNode.SelectSingleNode("//span[@title=input]").InnerText;
}
// Use this for initialization
void Start () {

}
}

运行时,我收到错误:

NullReferenceException: Object reference not set to an instance of an object
Dictionary.Translate (System.String input, System.String languagePair,System.Text.Encoding encoding) (at Assets/Dictionary.cs:32)
GUIScript.Start () (at Assets/GUIScript.cs:22)

我尝试过更改代码、查找解决方案、HtmlDocument 的 API 以及如何修复 NullReferenceExceptions,但出于某种原因,我无法弄清楚为什么我得到一个 NullReferenceException。这个问题已经困扰我一两周了,我需要继续我的项目。任何帮助将不胜感激!

最佳答案

如果我没记错的话,这是第 32 行:

return doc.DocumentNode.SelectSingleNode("//span[@title=input]").InnerText

这意味着 doc.DocumentNode 为 null 或 DocumentNode.SelectSingleNode("//span[@title=input]") 返回 null。

如果是前者,请检查您收到的是实际返回的文档。您的 URL 编码可能不正确。另见 why HTML Agility Pack HtmlDocument.DocumentNode is null?

如果是后者,则可能是 XPath 发生了一些奇怪的事情。我不知道这有多相关,因为 DocumentNode 应该是文档的根,讨论在 http://htmlagilitypack.codeplex.com/discussions/249129可以申请。据此,“//”是从文档的根开始搜索,您可能需要尝试 doc.DocumentNode.SelectSingleNode(".//span[@title=input] ") 而不是(将 . 添加到字符串的开头)。

调试方法并准确查看这些调用的值将完成这项工作。

关于c# - C# 中带有 HtmlDocument 引用的 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13031757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com