在 C# 中转义无效的 XML 字符 [英] Escape invalid XML characters in C#

查看:41
本文介绍了在 C# 中转义无效的 XML 字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含无效 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 字符的方法,我建议您使用 XmlConvert.IsXmlChar 方法.它是从 .NET Framework 4 开始添加的,也出现在 Silverlight 中.这是小样本:

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 = "vf";
    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 字符的方法,我建议您使用 XmlConvert.EncodeName 方法.这是小样本:

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 = "vf";
    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;
    }
}

更新:需要说明的是,编码操作产生的字符串的长度大于或等于源字符串的长度.当您将编码字符串存储在具有长度限制的数据库中的字符串列中并在您的应用中验证源字符串长度以适应数据列限制时,这可能很重要.

Update: It should be mentioned that the encoding operation produces a string with a length which is greater or equal than a length of a source string. It might 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆