如何在同一循环和行中获取Tag内部文本和属性值HtmlAgilityPack C#

问题描述 投票:0回答:1
html xpath attributes html-agility-pack phrase
1个回答
0
投票

以下内容对我有用!由于早期值不完整,因此空引用阻塞了我的代码。

希望这对某人有帮助!!

 foreach (var row in doc.DocumentNode.SelectNodes("//tr[td]")) 
 {
                        
     string pos = "0";
                         
    pos = row.SelectSingleNode("td//input")?.Attributes["name"].Value;
    string injid = row.SelectSingleNode("td//input")?.Attributes["value"].Value;

     table.Rows.Add((row.SelectNodes("td").Select(td => td.InnerText).ToArray()));

 if (string.IsNullOrEmpty(pos))
 {
     
 }
 else
 {
     int ronum = table.Rows.Count - 1;
    
     DataRow dr = table.Rows[ronum];
     dr[9] = pos;

 }

 if (string.IsNullOrEmpty(injid))
 {

 }
 else
 {
     int ronum = table.Rows.Count - 1;
  
     DataRow dr = table.Rows[ronum];
     dr[10] = injid;

 }
}
© www.soinside.com 2019 - 2024. All rights reserved.