Vb.net 替换html图像标记中的属性

Vb.net 替换html图像标记中的属性,vb.net,html-agility-pack,Vb.net,Html Agility Pack,我有1000多个包含html图像标记的数据库条目 问题是,90%的“src”属性只是占位符。我需要用适当的、真实的来源替换所有这些占位符 典型的数据库条目如下所示(图像标记的数量因条目而异): 一个怪物向你冲过来 怪物: 宝藏: 请在下面选择您的操作: 使用上面图像标签中的ID“d8fh4-gfkj3”和“x23zo-115a9”,我可以查询另一个函数以获取这些图像的“真实”源 因此,我尝试使用HtmlAgilityPack并得出以下结论: Dim doc作为新的HtmlDocument(

我有1000多个包含html图像标记的数据库条目

问题是,90%的“src”属性只是占位符。我需要用适当的、真实的来源替换所有这些占位符

典型的数据库条目如下所示(图像标记的数量因条目而异):

一个怪物向你冲过来

怪物:


宝藏:

请在下面选择您的操作:
使用上面图像标签中的ID“d8fh4-gfkj3”和“x23zo-115a9”,我可以查询另一个函数以获取这些图像的“真实”源

因此,我尝试使用HtmlAgilityPack并得出以下结论:

Dim doc作为新的HtmlDocument()
doc.LoadHtml(遇到文本)
对于doc.DocumentNode.SelectNodes(“//img”)中作为HtmlNode的每个imgTag
“拿身份证
Dim imgId作为HtmlAttribute=imgTag.Attributes(“id”)
Dim imageId作为字符串=imgId.Value
'获取新的/真实的路径
Dim newPath=getMediaPath(imageId)
Dim imgSrc作为HtmlAttribute=imgTag.Attributes(“src”)
“检查一下
但我不知道如何用新值替换旧值

有没有办法用HtmlAgilityPack做到这一点


谢谢

您应该能够只设置属性的值:

'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
    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
'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
doc.DocumentNode.OuterHtml