Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C中HtmlDocument引用的NullReferenceException#_C#_Unity Container_Html Agility Pack_Nullreferenceexception - Fatal编程技术网

C# C中HtmlDocument引用的NullReferenceException#

C# C中HtmlDocument引用的NullReferenceException#,c#,unity-container,html-agility-pack,nullreferenceexception,C#,Unity Container,Html Agility Pack,Nullreferenceexception,我正在使用HtmlAgilityPack从Google Translate中获取信息,用于翻译程序。我已经下载了HtmlAgilityPackdll,并在我的程序中成功引用了它。我在统一中使用集合。下面是我的两个程序代码: using UnityEngine; using System.Collections; using System; using System.Collections.Generic; using System.Linq; using System.Text; using S

我正在使用
HtmlAgilityPack
从Google Translate中获取信息,用于翻译程序。我已经下载了
HtmlAgilityPack
dll,并在我的程序中成功引用了它。我在统一中使用集合。下面是我的两个程序代码:

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));
    }
}
这是引用我的
字典
类的代码,如下所示:

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以及如何修复
NullReferenceException
,但由于某些原因,我无法理解为什么会出现
NullReferenceException
。这个问题已经让我耽搁了一两周,我需要继续我的项目。任何帮助都将不胜感激

如果我计算正确,这是第32行:

return doc.DocumentNode.SelectSingleNode("//span[@title=input]").InnerText
这意味着
doc.DocumentNode
为空或
DocumentNode.SelectSingleNode(//span[@title=input]”)
返回空值

如果是前者,请检查您是否收到了实际返回的文档。您的URL可能编码不正确。另见

如果是后者,XPath可能会出现一些奇怪的情况。我不知道这有多重要,因为
DocumentNode
应该是文档的根节点,可以应用上的讨论。根据这一点,“//”是从文档的根搜索,您可能需要尝试
doc.DocumentNode。选择SingleNode(“.//span[@title=input]”)
(将
添加到字符串的开头)


调试方法并准确查看这些调用的值将完成此任务。

您试图检索什么?我打开了您正在使用的url,对
title=input
进行了简单的查找,但没有返回任何内容。我想问一下,你在找霍拉的翻译,你好吗

如果是这样的话,我是在一个控制台应用程序中这样做的。希望这有帮助

    static void Main(string[] args)
    {
        string Input = "Hola";
        HtmlWeb web = new HtmlWeb();
        HtmlDocument doc = web.Load("http://www.google.com/translate_t?hl=en&ie=UTF8&text=Hola&langpair=es|en");

        string definition = doc.DocumentNode.SelectSingleNode(string.Format("//span[@title='{0}']",Input)).InnerText;
        Console.WriteLine(definition);
        Console.ReadKey();
    }

编辑:刚刚意识到您不是在寻找
title=input
,而是
title=Hola
。正如您在我的代码中看到的,请尝试
String.Format(“//span[@title='{0}'],Input)
。这将把变量
Input
的文本插入字符串行。

Document.SelectSingleNode无法编译,因为SelectSingleNode不是有效的方法。它需要DocumentNode才能访问SelectSingleNode。@CameronBarge抱歉,输入错误。我只是想在开头加一个点。