正如问题所暗示的那样,我正在尝试将字符串用作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运行。
答案 0 :(得分:1)
看起来你正在尝试使用C#string literal以这种方式构建字符串,如下所示:
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;