如何从 XML 中删除特殊字符 [英] How to remove special characters from a XML
本文介绍了如何从 XML 中删除特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 xml 文件.我想使用 C# 删除其中的所有特殊字符.
I have an xml file. I want to remove all of the special characters in it using C#.
特殊字符包括:
+
-
/
_
等
推荐答案
第一步:加载 Xml 文件到字符串
Step 1 : Load Xml file to string
public string ReadFileToString(string filePath)
{
StreamReader streamReader = new StreamReader(filePath);
string text = streamReader.ReadToEnd();
streamReader.Close();
return text;
}
Setp 2:使用函数去除所有特殊字符
Setp 2: Remove all the occurance of special char by using the function
public static string RemoveSpecialCharacters(string str)
{
//change regular expression as per your need
return Regex.Replace(str, "[^a-zA-Z0-9_.]", "", RegexOptions.Compiled);
}
Setp 3 : 保存文件
Setp 3 : Save file
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlstring);
doc.PreserveWhitespace = true;
doc.Save("data.xml");
这篇关于如何从 XML 中删除特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文