使用 HTMLAgility 包使用 C# 控制台应用程序从 Xpath 中提取值

Using HTMLAgility pack to extract value from a Xpath using c# console app

我有下面一行 HTML 代码,我使用 google chrome 作为 xpath。

<DIV id=TasheelPaymentCtrl1_dvPayment>
<TABLE border=1 cellSpacing=0 borderColor=black cellPadding=7 width=625 align=center>
<TBODY>
<TR>
<TD class=ReceiptHeadArbCenterHead1 width=320>المسمى </TD>
<TD class=ReceiptHeadArbCenterHead1 width=75>دفع إلى</TD>
<TD class=ReceiptHeadArbCenterHead1 width=75>القيمة</TD>
<TD class=ReceiptHeadArbCenterHead1 width=75>الكمية</TD>
<TD class=ReceiptHeadArbCenterHead1 width=75>المجموع</TD></TR>
<TR>
<TD class=ReceiptHeadArbCenterHead>رسوم وزارة العمل</TD>
<TD class=ReceiptValueArbCenter>MOFI</TD>
<TD class=ReceiptValueArbCenter>3</TD>
<TD class=ReceiptValueArbCenter>1</TD>
<TD class=ReceiptValueArbCenter>3</TD>
<TR>
<TD class=ReceiptHeadArbCenterHead>رسوم الدرهم الإلكتروني</TD>
<TD class=ReceiptValueArbCenter>MOFI</TD>
<TD class=ReceiptValueArbCenter>3</TD>
<TD class=ReceiptValueArbCenter>1</TD>
<TD class=ReceiptValueArbCenter>3</TD>
<TR>
<TD class=ReceiptHeadArbCenterHead>رسوم مراكز الخدمة </TD>
<TD class=ReceiptValueArbCenter>MOFI</TD>
<TD class=ReceiptValueArbCenter>47</TD>
<TD class=ReceiptValueArbCenter>1</TD>
<TD class=ReceiptValueArbCenter>47</TD>
<TR>
<TD class=ReceiptHeadArbCenterHead1 colSpan=4>المجموع</TD>
<TD class=ReceiptValueArbCenter>53</TD></TR></TBODY></TABLE></DIV>

我想提取值 3、3、47 和 53

我试过使用这个 xpath

 var gf = doc.DocumentNode.SelectNodes("//div[@id='TasheelPaymentCtrl1_dvPayment']/table/tbody/tr[2]/td[5]");

                foreach (var node in gf)
                {


                    Console.WriteLine(node.InnerText); //output: "3"
                }

                var sf = doc.DocumentNode.SelectNodes("//div[@id='TasheelPaymentCtrl1_dvPayment']/table/tbody/tr[3]/td[5]");

                foreach (var node in sf)
                {


                    Console.WriteLine(node.InnerText); //output: "3"
                }
                var tf = doc.DocumentNode.SelectNodes("//div[@id='TasheelPaymentCtrl1_dvPayment']/table/tbody/tr[4]/td[5]");

                foreach (var node in tf)
                {


                    Console.WriteLine(node.InnerText); //output: "47"
                }
var Allf = doc.DocumentNode.SelectNodes("//div[@id='TasheelPaymentCtrl1_dvPayment']/table/tbody/tr[5]/td[2]");

                foreach (var node in Allf )
                {


                    Console.WriteLine(node.InnerText); //output: "53"
                }

但我收到空对象异常.. 我使用 Google chrome 开发人员工具来复制 xpath。我收到空点异常。如何提取价值.. 我的问题是为什么我得到空点引用异常,xpath 值是否有错误? 请帮助我。

正如您所发现的,您的某些 XPath 表达式不起作用,因为 <tr> 标记未全部关闭。

因此,您需要在 XPath 表达式中满足这一点:

  • //div[@id='TasheelPaymentCtrl1_dvPayment']/table/tbody/tr[2]/td[5] - 没有变化
  • //div[@id='TasheelPaymentCtrl1_dvPayment']/table/tbody/tr[3]/td[5] - 应该是 //div[@id='TasheelPaymentCtrl1_dvPayment']/table/tbody/tr[2]/tr/td[5]
  • //div[@id='TasheelPaymentCtrl1_dvPayment']/table/tbody/tr[4]/td[5] - 应该是 //div[@id='TasheelPaymentCtrl1_dvPayment']/table/tbody/tr[2]/tr/tr/td[5]
  • //div[@id='TasheelPaymentCtrl1_dvPayment']/table/tbody/tr[5]/td[2] - 应该是 //div[@id='TasheelPaymentCtrl1_dvPayment']/table/tbody/tr[2]/tr/tr/tr/td[2]