【发布时间】: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 triggersDirectoryNotFoundException`。此外,OP 添加的不是``(我相信你知道这一点)。无论如何,您的评论将我引向下面的答案。谢谢
标签: c# visual-studio visual-studio-2015 html-agility-pack