You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure DevOps REST API评论字段含多余HTML标签问题求助

Fixing Extra HTML Tags in Azure DevOps Work Item Comments from REST API

Hey there! I’ve dealt with this exact headache before when pulling work item comments via the Azure DevOps REST API—those random HTML tags cluttering up the comment text make importing a total pain. Let’s go through a couple of solid solutions to get clean, usable comment content.

1. Quick Fix: Strip Tags with Regular Expressions

For straightforward cases where the HTML is simple (no nested tags or complex formatting), a regex can strip out all tags in one go. Here’s how to implement this in common languages:

Python Example

import re

def remove_html_tags(raw_comment):
    # Matches any HTML tag and replaces it with an empty string
    clean_pattern = re.compile(r'<.*?>')
    return re.sub(clean_pattern, '', raw_comment)

# Usage: pass the 'text' field from your API response here
clean_comment = remove_html_tags(api_response_comment['text'])

C# Example

using System.Text.RegularExpressions;

public static string StripHtml(string htmlContent)
{
    // Regex to eliminate all HTML tags
    return Regex.Replace(htmlContent, "<.*?>", string.Empty);
}

Note: Regex isn’t perfect for super complex HTML, but it works great for most Azure DevOps comment scenarios where tags are basic (like <p>, <br>, or <b>).

2. Robust Solution: Use an HTML Parsing Library

If you need to handle nested tags, preserve line breaks, or avoid edge cases regex misses, use a dedicated HTML parser. These libraries properly parse the HTML structure and extract just the text content.

Python (BeautifulSoup)

First install the package:

pip install beautifulsoup4

Then use it to clean comments:

from bs4 import BeautifulSoup

def clean_comment_text(html_comment):
    soup = BeautifulSoup(html_comment, "html.parser")
    # Get plain text, and replace <br> tags with actual newlines if needed
    return soup.get_text(separator='\n').strip()

C# (HtmlAgilityPack)

Install the NuGet package first, then:

using HtmlAgilityPack;

public static string ExtractCleanText(string htmlContent)
{
    var doc = new HtmlDocument();
    doc.LoadHtml(htmlContent);
    // Get inner text, which automatically strips HTML tags
    return doc.DocumentNode.InnerText.Trim();
}

This method is way more reliable for any HTML structure you might encounter in comments, including formatted text with line breaks or bold/italic styling.

3. Double-Check the API Response

Just a quick sanity check: the Azure DevOps REST API v6.1 Comments endpoint returns comment content in the text field, which is formatted with HTML. There’s no built-in plain-text option here, so cleaning the content post-retrieval is your only path forward.

Pick the method that fits your tech stack and comment complexity—for most teams, the HTML parsing library approach is the safest bet to avoid unexpected issues down the line.

内容的提问来源于stack exchange,提问作者Harsh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 04:57:39