I have an xml file. I开发者_Go百科 want to remove all of the special characters in it using C#.
The special characters include:
+
-
/
_
etc.
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: 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 : Save file
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlstring);
doc.PreserveWhitespace = true;
doc.Save("data.xml");
精彩评论