逃脱在C#中无效XML字符 [英] Escape invalid XML characters in C#
问题描述
我有一个包含无效XML字符的字符串。我怎样才能逃脱(或删除)无效XML字符之前我解析字符串?
I have a string that contains invalid XML characters. How can I escape (or remove) invalid XML characters before I parse the string?
推荐答案
由于方法来消除,我建议你使用无效XML字符<一href="http://msdn.microsoft.com/en-us/library/system.xml.xmlconvert.isxmlchar%28v=vs.100%29.aspx">XmlConvert.IsXmlChar方法。它,因为.NET Framework 4的加入,是psented在Silverlight太$ P $。这里是小样本:
As the way to remove invalid XML characters I suggest you to use XmlConvert.IsXmlChar method. It was added since .NET Framework 4 and is presented in Silverlight too. Here is the small sample:
void Main() {
string content = "\v\f\0";
Console.WriteLine(IsValidXmlString(content)); // False
content = RemoveInvalidXmlChars(content);
Console.WriteLine(IsValidXmlString(content)); // True
}
static string RemoveInvalidXmlChars(string text) {
var validXmlChars = text.Where(ch => XmlConvert.IsXmlChar(ch)).ToArray();
return new string(validXmlChars);
}
static bool IsValidXmlString(string text) {
try {
XmlConvert.VerifyXmlChars(text);
return true;
} catch {
return false;
}
}
和为逃避我建议你使用<无效XML字符的方式href="http://msdn.microsoft.com/en-us/library/system.xml.xmlconvert.en$c$cname%28v=vs.100%29.aspx">XmlConvert.En$c$cName方法。这里是小样本:
And as the way to escape invalid XML characters I suggest you to use XmlConvert.EncodeName method. Here is the small sample:
void Main() {
const string content = "\v\f\0";
Console.WriteLine(IsValidXmlString(content)); // False
string encoded = XmlConvert.EncodeName(content);
Console.WriteLine(IsValidXmlString(encoded)); // True
string decoded = XmlConvert.DecodeName(encoded);
Console.WriteLine(content == decoded); // True
}
static bool IsValidXmlString(string text) {
try {
XmlConvert.VerifyXmlChars(text);
return true;
} catch {
return false;
}
}
更新: 应该提到的是,编码操作产生的字符串的长度为大于或等于源串的长度。当你存储在数据库中的连接codeD字符串长度的限制字符串列和验证源字符串长度在你的应用程序,以适应数据列限制,它可以是非常重要的。
Update: It should be mentioned that the encoding operation produces a string with a length is greater or equal than a length of a source string. It can be important when you store a encoded string in a database in a string column with length limitation and validate source string length in your app to fit data column limitation.
这篇关于逃脱在C#中无效XML字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!