4

私はVS2010を使用しており、HTMLAGilityPack1.4.6(Net40フォルダーから)を使用しています。以下は私のHTMLです

<html>

<body>


<div id="header">

<h2 id="hd1">
    Patient Name
</h2>   
</div>
</body>


</html>

「hd1」にアクセスするために、C# で次のコードを使用しています。正しいやり方を教えてください。

HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
try
{
    string filePath = "E:\\file1.htm";
    htmlDoc.LoadHtml(filePath);

    if (htmlDoc.DocumentNode != null)
    { 

        HtmlNodeCollection _hdPatient = htmlDoc.DocumentNode.SelectNodes("//h2[@id=hd1]");
        // htmlDoc.DocumentNode.SelectNodes("//h2[@id='hd1']");  
        //_hdPatient.InnerHtml = "Patient SurName";
    }
}
catch (Exception ex)
{
    throw ex;
}

多くの順列と組み合わせを試しました...私はnullになります。

助けてください。

4

2 に答える 2

1

あなたはほとんどそこにいました:

HtmlNode _hdPatient = htmlDoc.DocumentNode.SelectSingleNode("//h2[@id='hd1']");
_hdPatient.InnerHtml = "Patient SurName"
于 2013-06-11T12:06:55.597 に答える