获取 XElement 的 InnerXml 的最佳方法? [英] Best way to get InnerXml of an XElement?

查看:25
本文介绍了获取 XElement 的 InnerXml 的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取下面代码中混合 body 元素内容的最佳方法是什么?该元素可能包含 XHTML 或文本,但我只想要字符串形式的内容.XmlElement 类型具有 InnerXml 属性,这正是我所追求的.

What's the best way to get the contents of the mixed body element in the code below? The element might contain either XHTML or text, but I just want its contents in string form. The XmlElement type has the InnerXml property which is exactly what I'm after.

编写的代码几乎做了我想要的,但包括周围的 ...元素,我不想要.

The code as written almost does what I want, but includes the surrounding <body>...</body> element, which I don't want.

XDocument doc = XDocument.Load(new StreamReader(s));
var templates = from t in doc.Descendants("template")
                where t.Attribute("name").Value == templateName
                select new
                {
                   Subject = t.Element("subject").Value,
                   Body = t.Element("body").ToString()
                };

推荐答案

我想看看这些建议的解决方案中哪一个表现最好,所以我进行了一些对比测试.出于兴趣,我还将 LINQ 方法与 Greg 建议的普通的旧 System.Xml 方法进行了比较.这种变化很有趣,并不是我所期望的,最慢的方法比最快的方法慢 3 倍以上.

I wanted to see which of these suggested solutions performed best, so I ran some comparative tests. Out of interest, I also compared the LINQ methods to the plain old System.Xml method suggested by Greg. The variation was interesting and not what I expected, with the slowest methods being more than 3 times slower than the fastest.

按最快到最慢排序的结果:

The results ordered by fastest to slowest:

  1. CreateReader - Instance Hunter(0.113 秒)
  2. 简单的旧 System.Xml - Greg Hurlman(0.134 秒)
  3. 使用字符串连接进行聚合 - Mike Powell(0.324 秒)
  4. StringBuilder - Vin(0.333 秒)
  5. String.Join on array - Terry(0.360 秒)
  6. 数组上的 String.Concat - Marcin Kosieradzki (0.364)

<小时>

方法

我使用了一个带有 20 个相同节点(称为提示")的 XML 文档:

I used a single XML document with 20 identical nodes (called 'hint'):

<hint>
  <strong>Thinking of using a fake address?</strong>
  <br />
  Please don't. If we can't verify your address we might just
  have to reject your application.
</hint>

上面显示的秒数是提取 20 个节点的内部 XML",连续 1000 次,并取 5 次运行的平均值(平均值)的结果.我没有包括将 XML 加载和解析为 XmlDocument(对于 System.Xml 方法)或 XDocument(对于所有其他人).

The numbers shown as seconds above are the result of extracting the "inner XML" of the 20 nodes, 1000 times in a row, and taking the average (mean) of 5 runs. I didn't include the time it took to load and parse the XML into an XmlDocument (for the System.Xml method) or XDocument (for all the others).

我使用的 LINQ 算法是:(C# - 都采用 XElement父"并返回内部 XML 字符串)

The LINQ algorithms I used were: (C# - all take an XElement "parent" and return the inner XML string)

创建阅读器:

var reader = parent.CreateReader();
reader.MoveToContent();

return reader.ReadInnerXml();

使用字符串连接进行聚合:

return parent.Nodes().Aggregate("", (b, node) => b += node.ToString());

StringBuilder:

StringBuilder sb = new StringBuilder();

foreach(var node in parent.Nodes()) {
    sb.Append(node.ToString());
}

return sb.ToString();

String.Join 数组:

return String.Join("", parent.Nodes().Select(x => x.ToString()).ToArray());

String.Concat 在数组上:

return String.Concat(parent.Nodes().Select(x => x.ToString()).ToArray());

我没有在这里展示Plain old System.Xml"算法,因为它只是在节点上调用 .InnerXml.

I haven't shown the "Plain old System.Xml" algorithm here as it's just calling .InnerXml on nodes.

结论

如果性能很重要(例如,大量 XML,经常解析),我会每次都使用 Daniel 的 CreateReader 方法.如果你只是做一些查询,你可能想使用 Mike 更简洁的 Aggregate 方法.

If performance is important (e.g. lots of XML, parsed frequently), I'd use Daniel's CreateReader method every time. If you're just doing a few queries, you might want to use Mike's more concise Aggregate method.

如果您在具有大量节点(可能有 100 个)的大型元素上使用 XML,您可能会开始看到使用 StringBuilder 而不是 Aggregate 方法的好处,但不会超过 创建阅读器.我不认为 JoinConcat 方法在这些情况下会更有效,因为将大列表转换为大数组的代价(甚至在这里很明显)较小的列表).

If you're using XML on large elements with lots of nodes (maybe 100's), you'd probably start to see the benefit of using StringBuilder over the Aggregate method, but not over CreateReader. I don't think the Join and Concat methods would ever be more efficient in these conditions because of the penalty of converting a large list to a large array (even obvious here with smaller lists).

这篇关于获取 XElement 的 InnerXml 的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆