选择 XML 元素而不使用扩展方法

问题描述 投票:0回答:1

MSDN - System.Xml.XPath 扩展类 说:

使用这些方法会带来一些性能损失。使用 LINQ to XML 查询可产生更好的性能。

并且 XPathSelectElement 是一个扩展方法

我有以下 XML。我需要找出这些消息并将其连接起来。挑战是 - 我需要只选择

Status/StatusMsg/StatusDetail
下的消息。通过
Descendants
,我可以获得所有消息 - 即使在所需元素之外。

这可以使用

XPathSelectElement
正确实现。但由于 XPathSelectElement 是一种扩展方法,因此它会产生一些性能影响,如 LINQ to XML with XPath Performance review 所示:

在大多数情况下,运行 XPath 查询的执行时间将比使用标准 LINQ to XML 的查询长 5 倍。

使用 C# 在 LINQ to XML 中不使用扩展方法的最佳方法是什么?

注意:有没有办法为此目的调整

Descendants

XML

XDocument xDoc = XDocument.Parse(@"  
          <Status>
                <StatusMsg>
                    <StatusType>INVOICE</StatusType>

                    <StatusDetail>
                        <Sequence test=""K"">  2  </Sequence>
                        <Message>A</Message>
                    </StatusDetail>

                    <StatusDetail>
                        <Message>B</Message>
                    </StatusDetail>

                    <StatusDetail>
                        <Message>C</Message>
                    </StatusDetail>
                </StatusMsg>

                    <StatusDetail>
                        <Message>OUTSIDE</Message>
                    </StatusDetail>
            </Status>
           ");

代码

// Descendants
var messageArrayWithOutside = xDoc.Descendants(@"StatusDetail")
                             .Select(
                                       x => x.Element("Message") == null ? String.Empty : x.Element("Message").Value.Trim()
                                    ).ToArray();

var textAll = string.Join(", ", messageArrayWithOutside);

//XPathSelectElements
var messageArray = xDoc.XPathSelectElements(@"Status/StatusMsg/StatusDetail")
                           .Select(
                                     x => x.Element("Message") == null ? String.Empty : x.Element("Message").Value.Trim()
                                  ).ToArray();

var text = string.Join(", ", messageArray);

更新

XPath 似乎比使用 Descendants 快两次。知道为什么吗?

        // Descendants
        Stopwatch stopWatchDescendants = new Stopwatch();
        stopWatchDescendants.Start();
        var messageArrayDecendants = xDoc.Descendants("StatusMsg")
            .Descendants("StatusDetail")
            .Select(
                x => x.Element("Message") == null ?string.Empty : x.Element("Message").Value.Trim()
            ).ToArray();

        var textDecendants = string.Join(", ", messageArrayDecendants);
        stopWatchDescendants.Stop();
        TimeSpan tsDescendants = stopWatchDescendants.Elapsed;


        //XPathSelectElements
        Stopwatch stopWatchXPath = new Stopwatch();
        stopWatchXPath.Start();
        var messageArrayXPath = xDoc.XPathSelectElements(@"Status/StatusMsg/StatusDetail")
                           .Select(
                                     x => x.Element("Message") == null ? String.Empty : x.Element("Message").Value.Trim()
                                  ).ToArray();

        var textXPath = string.Join(", ", messageArrayXPath);
        stopWatchXPath.Stop();
        TimeSpan tsXPath = stopWatchXPath.Elapsed;


        if (tsXPath > tsDescendants)
        {
            Console.WriteLine("LINQ is fast");
        }
        if (tsDescendants > tsXPath)
        {
            Console.WriteLine("XPath is fast");
        }

        Console.WriteLine("XPath :" + tsXPath.ToString());
        Console.WriteLine("LINQ :" + tsDescendants.ToString());
c# xml linq
1个回答
3
投票

您需要使用而不是像下面这样:

该字符串将包含所需的输出

关键似乎是使用限制了搜索

© www.soinside.com 2019 - 2024. All rights reserved.