检测XML的更好方法? [英] Better way to detect XML?
问题描述
当前,我有以下c#代码可从文本中提取值.如果是XML,则需要其中的值;否则,如果不是XML,则可以返回文本本身.
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;
}
我知道C#中的异常代价很高,所以我想知道是否有更好的方法来确定我正在处理的文本是否为xml?
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.
推荐答案
您可以对<因为所有XML都必须以1开头,而所有非XML的大部分都不会以1开头.
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.
(手写).
// 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;
}
我最初使用的是正则表达式,但是Trim()[0]与该正则表达式相同.
I originally had the use of a regex but Trim()[0] is identical to what that regex would do.
这篇关于检测XML的更好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!