Better way to detect XML?(检测 XML 的更好方法?)
问题描述
目前,我有以下 c# 代码从文本中提取值.如果是 XML,我想要其中的值 - 否则,如果不是 XML,它可以只返回文本本身.
字符串数据 = "..."尝试{返回 XElement.Parse(data).Value;}捕捉(System.Xml.XmlException){返回数据;}
我知道异常在 C# 中很昂贵,所以我想知道是否有更好的方法来确定我正在处理的文本是否为 xml?
我想到了正则表达式测试,但我不认为这是一个更便宜的选择.请注意,我要求的是一种更便宜的方法.
你可以对
(手写.)
//长度必须是 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 的更好方法?
基础教程推荐
- 将 XML 转换为通用列表 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01