c# - 从 Html Img 标签中检索 Url

标签 c# string html-agility-pack

背景信息

目前正在开发一个 C# web api,它将返回选定的 Img url 作为 base64。我目前具有执行 base64 转换的功能,但是,我收到了大量文本,其中还包括 Img Url,我需要从字符串中裁剪掉这些文本并将其提供给我的函数以将 img 转换为 base 64。我阅读了一个 lib.("HtmlAgilityPack;") 应该可以使这个任务变得简单但是当我使用它时我找不到“HtmlDocument.cs”。但是,我不是在提交文档,而是向它发送一个 HTML 字符串。我阅读了文档,它应该也可以使用字符串,但它对我不起作用。这是使用“HtmlAgilityPack”的代码。

非工作代码

foreach(var item in returnList)
                    {
                         if (item.Content.Contains("~~/picture~~"))
                        {
                            HtmlDocument doc = new HtmlDocument();
                            doc.Load(item.Content);

来自 HtmlAgilityPack 的错误消息

enter image description here

问题 我从 SharePoint 收到一个 Html 字符串。此 Html 字符串可以使用标题标记和/或图片标记进行标记。我正在尝试从 img src Hmtl 标记中分离检索 html。我知道正则表达式可能不切实际,但我会考虑使用正则表达式是否可以从 img src 检索 url。

示例字符串

Bullet~~Increased Cash Flow</li><li>~~/Document Text Bullet~~Tax Efficient Organizational Structures</li><li>~~/Document Text Bullet~~Tax Strategies that Closely Align with Business Strategies</li><li>~~/Document Text Bullet~~Complete Knowledge of State and Local Tax Obligations</li></ul><p>~~/Document Heading 2~~is the firm of choice</p><p>~~/Document Text~~When it comes to accounting and advisory services is the unique firm of choice. As a trusted advisor to our clients, we bring an integrated client service approach with dedicated industry experience. Dixon Hughes Goodman respects the value of every client relationship and provides clients throughout the U.S. with an unwavering commitment to hands-on, personal attention from our partners and senior-level professionals.</p><p>~~/Document Text~~of choice for clients in search of a trusted advisor to deal with their state and local tax needs. Through our leading best practices and experience, our SALT professionals offer quality and ease to the client engagement. We are proud to provide highly comprehensive services.</p>

    <p>~~/picture~~<br></p><p> 
          <img src="/sites/ContentCenter/Graphics/map-al.jpg" alt="map al" style="width&#58;611px;height&#58;262px;" />&#160;
    <br></p><p><br></p><p>
    ~~/picture~~<br></p><p>
          <img src="/sites/ContentCenter/Graphics/Firm_Telescope_Illustration.jpg" alt="Firm_Telescope_Illustration.jpg" style="margin&#58;5px;width&#58;155px;height&#58;155px;" />    </p><p></div><div class="ExternalClassAF0833CB235F437993D7BEE362A1A88A"><br></div><div class="ExternalClassAF0833CB235F437993D7BEE362A1A88A"><br></div><div class="ExternalClassAF0833CB235F437993D7BEE362A1A88A"><br></div>

重要

我正在使用 HTML 字符串,而不是文件。

最佳答案

您遇到的问题是 C# 正在寻找一个文件,但由于没有找到,它会告诉您。这不是一个会阻止您的应用程序的错误,它只是告诉您找不到该文件,然后库将读取给定的字符串。此文档可在此处找到 https://htmlagilitypack.codeplex.com/SourceControl/latest#Trunk/HtmlAgilityPackDocumentation.shfbproj .下面的代码是任何人都可以使用的千篇一律模型。

重要

C# 正在查找无法显示的文件,因为它是提供的字符串。这就是您收到的消息,但是您仍然可以根据提供的文档正常工作,并且不会影响您的代码。

示例代码

HtmlAgilityPack.HtmlDocument htmlDocument = new HtmlAgilityPack.HtmlDocument();
htmlDocument.LoadHtml("YourContent"); // can be a string or can be a path.

HtmlAttribute att = url.Attributes["src"];
Uri imgUrl = new System.Uri("Url"+ att.Value); // build your url

关于c# - 从 Html Img 标签中检索 Url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38376540/

相关文章:

c# - 生成随机颜色并排除旧颜色的最佳方法

c# - 当我通过 EF 向 SQL 添加值时,ID 始终为 0

c - C 中的字符串处理——指针和引用调用问题

string - 计算重复字符的迭代器适配器

c# - 将 html 表格/图表元素转换为图像

c# - 将数据更新到 XML 文件 c#

c# - 从其他脚本Unity访问脚本

c++ - 我无法用 wcout 输出字符串

c# - 使用 HAP 添加样式表

xpath - 使用HTML Agility Pack从特定节点捕获内部文本