检测 XML 的更好方法?

Better way to detect XML?(检测 XML 的更好方法?)

本文介绍了检测 XML 的更好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有以下 c# 代码从文本中提取值.如果是 XML,我想要其中的值 - 否则,如果不是 XML,它可以只返回文本本身.

字符串数据 = "..."尝试{返回 XElement.Parse(data).Value;}捕捉(System.Xml.XmlException){返回数据;}

我知道异常在 C# 中很昂贵,所以我想知道是否有更好的方法来确定我正在处理的文本是否为 xml?

我想到了正则表达式测试,但我不认为这是一个更便宜的选择.请注意,我要求的是一种更便宜的方法.

解决方案

你可以对

做一个初步的检查.因为所有 XML 都必须以 1 开头,而所有非 XML 的大部分都不会以 1 开头.

(手写.)

//长度必须是 XMLif (!string.IsNullOrEmpty(data)){//如果它以 < 开头修剪后可能是XML//如果字符串全是空格,需要再次进行空检查.var trimmedData = data.TrimStart();if (string.IsNullOrEmpty(trimmedData)){返回数据;}if (trimmedData[0] == '<'){尝试{返回 XElement.Parse(data).Value;}捕捉(System.Xml.XmlException){返回数据;}}}别的{返回数据;}

我最初使用的是正则表达式,但 Trim()[0] 与该正则表达式的作用相同.

Currently, I have the following c# code to extract a value out of text. If its XML, I want the value within it - otherwise, if its not XML, it can just return the text itself.

String data = "..."
try
{
    return XElement.Parse(data).Value;
}
catch (System.Xml.XmlException)
{
    return data;
}

I know exceptions are expensive in C#, so I was wondering if there was a better way to determine if the text I'm dealing with is xml or not?

I thought of regex testing, but I dont' see that as a cheaper alternative. Note, I'm asking for a less expensive method of doing this.

解决方案

You could do a preliminary check for a < since all XML has to start with one and the bulk of all non-XML will not start with one.

(Free-hand written.)

// Has to have length to be XML
if (!string.IsNullOrEmpty(data))
{
    // If it starts with a < after trimming then it probably is XML
    // Need to do an empty check again in case the string is all white space.
    var trimmedData = data.TrimStart();
    if (string.IsNullOrEmpty(trimmedData))
    {
       return data;
    }

    if (trimmedData[0] == '<')
    {
        try
        {
            return XElement.Parse(data).Value;
        }
        catch (System.Xml.XmlException)
        {
            return data;
        }
    }
}
else
{
    return data;
}

I originally had the use of a regex but Trim()[0] is identical to what that regex would do.

这篇关于检测 XML 的更好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:检测 XML 的更好方法?

基础教程推荐