需要一种从String中删除非法XML字符的方法 [英] Need a method that removes illegal XML characters from a String
本文介绍了需要一种从String中删除非法XML字符的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请告诉我如何使用C#代码从xml文件中删除这个隐形垃圾字符
我想读一些xml文件。当我读到我发现一些不需要的字符,如符号presenet在其中我需要删除它,任何1可以帮助我
Please tell me how can i remove this invisible junk characters from xml file using C# code
I want to read some xml files. when i read i found some unwanted characters like symbols presenet in it i need to remove it, can any 1 helps me
推荐答案
internal static void RectifyXML()
{
//the path to the xml file
string path = @"C:\CodeProject\test.xml";
//create the xmldocument
System.Xml.XmlDocument CXML = new System.Xml.XmlDocument();
//load the xml into the XmlDocument
CXML.Load(path);
string correctedXMlString = Regex.Replace(CXML.InnerXml, @"[^\u0000-\u007F]", string.Empty);
File.Delete(path);
CXML.LoadXml(correctedXMlString);
CXML.Save(path);
}
var xmlPattern = "[^\u0001-\uD7FF\uE000-\uFFFD\ud800\udc00-\udbff\udfff]";
var newXml = xml.replace(new RegExp(xmlPattern , "g"), "");
这篇关于需要一种从String中删除非法XML字符的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文