在构造使用数据的 XmlReader 或 XPathDocument 之前,如何从基于 XML 的数据源中删除无效的十六进制字符? [英] How do you remove invalid hexadecimal characters from an XML-based data source prior to constructing an XmlReader or XPathDocument that uses the data?

查看:25
本文介绍了在构造使用数据的 XmlReader 或 XPathDocument 之前,如何从基于 XML 的数据源中删除无效的十六进制字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 XmlReader 中使用基于 XML 的数据源之前,是否有任何简单/通用的方法来清理它,以便我可以优雅地使用不符合对 XML 施加的十六进制字符限制的 XML 数据?

Is there any easy/general way to clean an XML based data source prior to using it in an XmlReader so that I can gracefully consume XML data that is non-conformant to the hexadecimal character restrictions placed on XML?

注意:

  • 解决方案需要处理 XML使用字符的数据源UTF-8 以外的编码,例如经过指定字符编码在XML 文档声明.不是修改字符编码源同时剥离无效十六进制字符已被主要症结所在.
  • 删除无效的十六进制字符应该只删除十六进制编码的值,因为您经常可以在数据中找到 href 值,而这些值恰好包含一个字符串,而该字符串与十六进制字符的字符串匹配.

背景:

我需要使用符合特定格式(例如 Atom 或 RSS 提要)的基于 XML 的数据源,但希望能够使用已发布的数据源,这些数据源包含符合 XML 规范的无效十六进制字符.

I need to consume an XML-based data source that conforms to a specific format (think Atom or RSS feeds), but want to be able to consume data sources that have been published which contain invalid hexadecimal characters per the XML specification.

在 .NET 中,如果您有一个表示 XML 数据源的 Stream,然后尝试使用 XmlReader 和/或 XPathDocument 解析它,则会由于 XML 数据中包含无效的十六进制字符而引发异常.我目前解决此问题的尝试是将 Stream 解析为字符串并使用正则表达式删除和/或替换无效的十六进制字符,但我正在寻找更高效的解决方案.

In .NET if you have a Stream that represents the XML data source, and then attempt to parse it using an XmlReader and/or XPathDocument, an exception is raised due to the inclusion of invalid hexadecimal characters in the XML data. My current attempt to resolve this issue is to parse the Stream as a string and use a regular expression to remove and/or replace the invalid hexadecimal characters, but I am looking for a more performant solution.

推荐答案

可能并不完美(强调是因为人们错过了此免责声明),但我在这种情况下所做的如下.您可以调整以与流一起使用.

It may not be perfect (emphasis added since people missing this disclaimer), but what I've done in that case is below. You can adjust to use with a stream.

/// <summary>
/// Removes control characters and other non-UTF-8 characters
/// </summary>
/// <param name="inString">The string to process</param>
/// <returns>A string with no control characters or entities above 0x00FD</returns>
public static string RemoveTroublesomeCharacters(string inString)
{
    if (inString == null) return null;

    StringBuilder newString = new StringBuilder();
    char ch;

    for (int i = 0; i < inString.Length; i++)
    {

        ch = inString[i];
        // remove any characters outside the valid UTF-8 range as well as all control characters
        // except tabs and new lines
        //if ((ch < 0x00FD && ch > 0x001F) || ch == '	' || ch == '
' || ch == '
')
        //if using .NET version prior to 4, use above logic
        if (XmlConvert.IsXmlChar(ch)) //this method is new in .NET 4
        {
            newString.Append(ch);
        }
    }
    return newString.ToString();

}

这篇关于在构造使用数据的 XmlReader 或 XPathDocument 之前,如何从基于 XML 的数据源中删除无效的十六进制字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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