开发者

How to remove special characters from a XML

开发者 https://www.devze.com 2023-03-17 19:22 出处:网络
I have an xml file. I开发者_Go百科 want to remove all of the special characters in it using C#.

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

精彩评论

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