【问题标题】:How to use a string as an HtmlAgilityPack Document path?如何使用字符串作为 HtmlAgilityPack 文档路径?
【发布时间】:2016-03-25 00:17:18
【问题描述】:

正如问题所暗示的,我正在尝试使用字符串作为 HtmlAgilityPack DocumentNode 路径。当我运行它说的代码时;路径中有非法字符。

我的代码:

类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using System.IO;
using System.Dynamic;

namespace Class_xlAccountSCRead_ClassBuild
{
    public class xlAccount_SCProcess
    {
        public static string ascDir { get; set; }
        public static string ascFav { get; set; }
        public static string accountSourceCodeDir;

        public static void ascRead()
        {
            HtmlAgilityPack.HtmlDocument docc = new HtmlAgilityPack.HtmlDocument();
            HtmlDocument doc = new HtmlDocument();
            doc.Load(ascDir);
            ascFav = doc.DocumentNode.SelectNodes("//*[@id=\"favoritesContent\"]/div[2]/div[2]/ul")[0].InnerHtml;


        }

    }


}

表格:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Dynamic;
using HtmlAgilityPack;

namespace xlAccountSCRead_ClassBuild
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            Class_xlAccountSCRead_ClassBuild.xlAccount_SCProcess.ascDir = "@" + "\"" + textBox1.Text + "\"";

            //await Task.Delay(100);
            Class_xlAccountSCRead_ClassBuild.xlAccount_SCProcess.ascRead();

            textBox2.Text = Class_xlAccountSCRead_ClassBuild.xlAccount_SCProcess.ascFav;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Class_xlAccountSCRead_ClassBuild.xlAccount_SCProcess.ascDir = "@" + "\"" +  textBox1.Text + "\"";

            label1.Text = Class_xlAccountSCRead_ClassBuild.xlAccount_SCProcess.ascDir;
        }
    }
}

进程由命名空间 Class_xlAccountSCRead_ClassBuild 中的 ascRead 运行。

【问题讨论】:

  • 不清楚您所说的 DocumentNode 路径 是什么意思。 doc.Load(ascDir); 行是否抛出“非法字符”错误?如果是这样,您能否发布错误发生时ascDir 的确切值?
  • @har07 因为 OP 明确在值的末尾添加了斜杠,所以这个错误是完全可以预料的......不太确定应该如何修复它。
  • @AlexeiLevenkov 认为你是对的。我试图通过运行简单的测试代码来确认,但结果却是` at the end of a path triggers DirectoryNotFoundException`。此外,OP 添加的不是``(我相信你知道这一点)。无论如何,您的评论将我引向下面的答案。谢谢

标签: c# visual-studio visual-studio-2015 html-agility-pack


【解决方案1】:

看起来您正在尝试以使用 C# 字符串文字的方式构建字符串,如下所示:

string cs_literal = @"the_actual_path_goes_here";

请注意这里的代码...

string your_string = "@" + "\"" +  textBox1.Text + "\"";

...将生成与此 C# 字符串文字等效的内容:

string cs_literal = "@\"the_actual_path_goes_here\"";

话虽如此,引号在路径中是非法的,因此“路径中的非法字符”异常。实际上,您不需要在此处进行所有转义,因为您在 C# 代码中键入的不是字符串文字。只需从TextBox 传递字符串按原样

string your_string = textBox1.Text;

【讨论】:

  • 谢谢伙计(或女孩),您的建议解决了我的问题。不敢相信我没有尝试过。
猜你喜欢
  • 2014-03-22
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-10
  • 2023-04-04
相关资源
最近更新 更多