开发者

How to remove special characters from a XML

I have an xml file. I开发者_Go百科 want to remove all of the special characters in it using C#.

The special characters include:

  1. +
  2. -
  3. /
  4. _

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");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜