Reputation: 7092
I have a 1000+ database entries that contain html image tags.
The problem is, 90% of the 'src' attributes are just placeholders. I need to replace all of those placeholders with the appropriate, real sources.
A typical database entry looks like this(the amount of image tags vary from entry to entry):
<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 />
Using the IDs in the image tags above, 'd8fh4-gfkj3' & 'x23zo-115a9', I can query another function to get the "real" sources for those images.
So I tried using HtmlAgilityPack and came up with this(below):
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
But I can't figure out how to actually replace the old value with the new value.
Is there a way to do this with the HtmlAgilityPack?
Thanks!
Upvotes: 1
Views: 360
Reputation: 8160
You should be able to just set the value for the attribute:
'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
After the replacement, you can get the updated HTML with:
doc.DocumentNode.OuterHtml
Upvotes: 1