我正在使用HtmlAgilityPack
以便从翻译工具的Google Translate中抓取信息。我已经下载了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。如果是前者,请检查是否收到了实际的文件。您的网址可能未正确编码。另见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/