<small id='MmHRg'></small><noframes id='MmHRg'>

<legend id='MmHRg'><style id='MmHRg'><dir id='MmHRg'><q id='MmHRg'></q></dir></style></legend>

      • <bdo id='MmHRg'></bdo><ul id='MmHRg'></ul>
    1. <tfoot id='MmHRg'></tfoot>
    2. <i id='MmHRg'><tr id='MmHRg'><dt id='MmHRg'><q id='MmHRg'><span id='MmHRg'><b id='MmHRg'><form id='MmHRg'><ins id='MmHRg'></ins><ul id='MmHRg'></ul><sub id='MmHRg'></sub></form><legend id='MmHRg'></legend><bdo id='MmHRg'><pre id='MmHRg'><center id='MmHRg'></center></pre></bdo></b><th id='MmHRg'></th></span></q></dt></tr></i><div id='MmHRg'><tfoot id='MmHRg'></tfoot><dl id='MmHRg'><fieldset id='MmHRg'></fieldset></dl></div>

    3. 检测 XML 的更好方法?

      时间:2023-08-28

    4. <tfoot id='vg6dQ'></tfoot>
      <legend id='vg6dQ'><style id='vg6dQ'><dir id='vg6dQ'><q id='vg6dQ'></q></dir></style></legend>

      <small id='vg6dQ'></small><noframes id='vg6dQ'>

          <tbody id='vg6dQ'></tbody>
          <bdo id='vg6dQ'></bdo><ul id='vg6dQ'></ul>
          <i id='vg6dQ'><tr id='vg6dQ'><dt id='vg6dQ'><q id='vg6dQ'><span id='vg6dQ'><b id='vg6dQ'><form id='vg6dQ'><ins id='vg6dQ'></ins><ul id='vg6dQ'></ul><sub id='vg6dQ'></sub></form><legend id='vg6dQ'></legend><bdo id='vg6dQ'><pre id='vg6dQ'><center id='vg6dQ'></center></pre></bdo></b><th id='vg6dQ'></th></span></q></dt></tr></i><div id='vg6dQ'><tfoot id='vg6dQ'></tfoot><dl id='vg6dQ'><fieldset id='vg6dQ'></fieldset></dl></div>
                本文介绍了检测 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 的更好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                <legend id='w7aG8'><style id='w7aG8'><dir id='w7aG8'><q id='w7aG8'></q></dir></style></legend>
                <i id='w7aG8'><tr id='w7aG8'><dt id='w7aG8'><q id='w7aG8'><span id='w7aG8'><b id='w7aG8'><form id='w7aG8'><ins id='w7aG8'></ins><ul id='w7aG8'></ul><sub id='w7aG8'></sub></form><legend id='w7aG8'></legend><bdo id='w7aG8'><pre id='w7aG8'><center id='w7aG8'></center></pre></bdo></b><th id='w7aG8'></th></span></q></dt></tr></i><div id='w7aG8'><tfoot id='w7aG8'></tfoot><dl id='w7aG8'><fieldset id='w7aG8'></fieldset></dl></div>

                <small id='w7aG8'></small><noframes id='w7aG8'>

                    <tbody id='w7aG8'></tbody>
                  <tfoot id='w7aG8'></tfoot>

                      • <bdo id='w7aG8'></bdo><ul id='w7aG8'></ul>