2017-05-04 16 views
1

htmlイメージタグを含む1000以上のデータベースエントリがあります。htmlイメージタグ内の属性を置き換えます

問題は、 'src'属性の90%が単なるプレースホルダです。私はそれらのプレースホルダーのすべてを適切な、実際の情報源に置き換える必要があります。

<p>A monster rushes at you!</p> 
Monster:<p><img id="d8fh4-gfkj3" src="(image_placeholder)" /></p> 
<br /> 
Treasure: <p><img id="x23zo-115a9" src="(image_placeholder)" /></p> 
Please select your action below: 
</br /> 

上記イメージタグ内のIDを使用して、「d8fh4-gfkj3」&「x23zo-115a9:一般的なデータベース・エントリは次のようになり

(イメージタグの量は、エントリからのエントリに変えます) '、私はそれらの画像のための "本当の"情報源を得るために別の関数を問い合わせることができます。

だから私はHtmlAgilityPackを使用してみましたし、この思い付いた(下):

Dim doc As New HtmlDocument() 
    doc.LoadHtml(encounterText) 

    For Each imgTag As HtmlNode In doc.DocumentNode.SelectNodes("//img") 
     'get the ID 
     Dim imgId As HtmlAttribute = imgTag.Attributes("id") 
     Dim imageId As String = imgId.Value 

     'get the new/real path 
     Dim newPath = getMediaPath(imageId) 
     Dim imgSrc As HtmlAttribute = imgTag.Attributes("src") 

     'check to see if the <img> tag "src" attribute has a placeholder 
     If imgSrc.Value.Contains("(image_placeholder)") Then 
      'replace old image src attribute with 'src=newPath' 
     End If 
    Next 

しかし、私は実際には新しい値で古い値を交換する方法を見つけ出すことはできません。

HtmlAgilityPackでこれを行う方法はありますか?

ありがとうございます!

答えて

1

あなただけの属性の値を設定することができるはずです。

doc.DocumentNode.OuterHtml 

'check to see if the <img> tag "src" attribute has a placeholder 
If imgSrc.Value.Contains("(image_placeholder)") Then 
    'replace old image src attribute with 'src=newPath' 
    imgSrc.Value = newPath 
End If 

交換後、あなたが更新されたHTMLを取得することができます