[英]Using HTMLAgility pack to extract value from a Xpath using c# console app
I have the following line of HTML code and I used google chrome for xpath. 我有以下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>
I want to extract values 3, 3, 47 and 53 我想提取值3、3、47和53
I tried using this xpath 我尝试使用此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"
}
but i am getting null object exception.. I used Google chrome developer tools to copy the xpath. 但是我收到了空对象异常。。我使用了Google chrome开发人员工具来复制xpath。 I am getting null point exception .
我收到空值异常。 How can extract value .. My question is why I am getting null point reference exception, is there any mistake in xpath value?
如何提取值..我的问题是为什么我会得到空点引用异常,xpath值中有任何错误? Please help me.
请帮我。
As you have discovered, some of your XPath expressions don't work because the <tr>
tags are not all closed. 您已经发现,某些XPath表达式不起作用,因为
<tr>
标记并未全部关闭。
Therefore, you will need to cater for this in your XPath expressions: 因此,您需要在XPath表达式中满足此要求:
//div[@id='TasheelPaymentCtrl1_dvPayment']/table/tbody/tr[2]/td[5]
- no change //div[@id='TasheelPaymentCtrl1_dvPayment']/table/tbody/tr[2]/td[5]
-不变 //div[@id='TasheelPaymentCtrl1_dvPayment']/table/tbody/tr[3]/td[5]
- should be //div[@id='TasheelPaymentCtrl1_dvPayment']/table/tbody/tr[2]/tr/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]
- should be //div[@id='TasheelPaymentCtrl1_dvPayment']/table/tbody/tr[2]/tr/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]
- should be //div[@id='TasheelPaymentCtrl1_dvPayment']/table/tbody/tr[2]/tr/tr/tr/td[2]
//div[@id='TasheelPaymentCtrl1_dvPayment']/table/tbody/tr[5]/td[2]
-应该是//div[@id='TasheelPaymentCtrl1_dvPayment']/table/tbody/tr[2]/tr/tr/tr/td[2]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.