C# 函数从xml字符串中排除特定标记
我给了XML字符串一个html标记列表,如“C# 函数从xml字符串中排除特定标记,c#,C#,我给了XML字符串一个html标记列表,如“,”等 现在我想创建一个通用函数,在这里我将传递html标记列表,或者也可以是一个标记,我想从传递的XML字符串中排除它。函数将返回整个字符串,而不包含排除的标记 public const String[] htmlTags = new String[] { "<p>", "a", "img" }; string result = strString.ExcludeHTMLTags(htmlTags); //I will write
,
”等
现在我想创建一个通用函数,在这里我将传递html标记列表,或者也可以是一个标记,我想从传递的XML字符串中排除它。函数将返回整个字符串,而不包含排除的标记
public const String[] htmlTags = new String[] { "<p>", "a", "img" };
string result = strString.ExcludeHTMLTags(htmlTags); //I will write the String extension not an issue, please suggest how to exclude tags from exisiting string.
public const String[]htmlTags=新字符串[]{“”,“a”,“img”};
字符串结果=strString.ExcludeHTMLTags(htmlTags)//我将编写字符串扩展名,这不是问题,请建议如何从现有字符串中排除标记。
编辑:
我正在尝试以下代码:
/// <summary>
/// Remove HTML tags from string using char array.
/// </summary>
public static string StripTagsCharArray(string source, String[] htmlTags)
{
char[] array = new char[source.Length];
int arrayIndex = 0;
bool inside = false;
for (int i = 0; i < source.Length; i++)
{
foreach (String htmlTag in htmlTags)
{
char let = source[i];
String tag = "<" + "htmlTag"; //How to handle this as this is character
if (let == tag)
{
inside = true;
continue;
}
if (let == '>')
{
inside = false;
continue;
}
if (!inside)
{
array[arrayIndex] = let;
arrayIndex++;
}
}
}
return new string(array, 0, arrayIndex);
}
//
///使用字符数组从字符串中删除HTML标记。
///
公共静态字符串stripTagScharray(字符串源,字符串[]htmlTags)
{
char[]数组=新字符[source.Length];
int-arrayIndex=0;
bool-inside=false;
for(int i=0;i String tag=“将html转换为DOM树并删除名称包含在给定的排除标记列表中的元素节点您是否尝试过。它是一种敏捷的html解析器,用于构建读/写DOM,并支持纯XPATH或XSLT,构建为.NET代码库,允许您在web之外进行解析"HTML文件,您可以按照您想要的方式修复字符串,修改DOM,添加节点,复制节点,以及。我们是否有任何功能可以完成上述操作?任何示例您都可以使用DOMParser。以下示例:传递我想从xml字符串中排除的htmls标记列表如何。将您有问题的代码显示出来,然后就可以为了帮助;)在这里添加了我的示例代码,我尝试读取每个字符,但是我需要检查整个标记并删除它。添加了一些代码尝试,但是有一些疑问
String[] htmlTags = new String[] { "a", "img", "p" };
private const string STR_RemoveHtmlTagRegex = "</?{0}[^<]*?>";
public static string RemoveHtmlTag(String input, String[] htmlTags)
{
String strResult = String.Empty;
foreach (String htmlTag in htmlTags)
{
Regex reg = new Regex(String.Format(STR_RemoveHtmlTagRegex, htmlTag.Trim()), RegexOptions.IgnoreCase);
strResult = reg.Replace(input, String.Empty);
input = strResult;
}
return strResult;
}