得到一个的XElement的InnerXml最好的方法? [英] Best way to get InnerXml of an XElement?

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

问题描述

什么是得到的混合元素的含量低于code的最佳方法是什么?该元素可以包含任何XHTML或文字,但我只是想它的字符串形式的内容。该的XmlElement 类型具有 InnerXml 属性这正是我后。

在code所写的几乎的我想要做什么,但包括周围的<身体GT; ... < /身体GT; 元素,这是我不希望

的XDocument DOC = XDocument.Load(新的StreamReader(S)); VAR模板从t =在doc.Descendants(模板)                 其中,t.Attribute(名)。价值== TEMPLATENAME                 选择新                 {                    主题= t.Element(主题),价值,                    车身= t.Element(身体)。的ToString()                 };

解决方案

我想看到这些建议的解决方案中表现最好的,所以我跑了一些对比​​试验。出于兴趣,我也比较LINQ的方法对普通的旧的的System.Xml 方式由格雷格建议。的变化是有趣的,不出我所料,最慢的方法是 3倍以上,比最快的慢

结果订购的最快最慢的:

  1. CreateReader - 副本猎人(0.113秒)
  2. 在平原旧的System.Xml - 格雷格Hurlman(0.134秒)
  3. 与总字符串连接迈克 - 鲍威尔(0.324秒)
  4. 的StringBuilder - VIN(0.333秒)
  5. 的string.join的阵列 - 特里(0.360秒)
  6. 在String.Concat的阵列 - 马辛Kosieradzki(0.364)

方法

我曾经与20相同的节点(称为提示),一个XML文档:

 <提示>
  <强>使用假地址&LT的思考; / STRONG>
  < BR />
  请不要。如果我们无法确认您的地址,我们可能只是
  有权拒绝你的申请。
< /提示>
 

示为秒的数字以上是提取20个节点,1000次的内部XML成一排,并利用5奔跑的平均(均值)的结果。我不包括所花费的加载和分析XML转换成时间的XmlDocument (用于在的System.Xml 法)或的XDocument (所有其他)。

LINQ的算法,我用是:(C# - 都需要一个的XElement 父,回到内XML字符串)

CreateReader:

VAR读卡器= parent.CreateReader(); reader.MoveToContent(); 返回reader.ReadInnerXml();

与总字符串连接:

返回parent.Nodes()骨料(,(B,节点)=> B + =节点。的ToString());

StringBuilder的:

StringBuilder的SB =新的StringBuilder(); 的foreach(VAR在parent.Nodes)节点(){     sb.Append(node.ToString()); } 返回sb.ToString();

在阵列的string.join:

返回的string.join(,parent.Nodes()选择(X => x.ToString() ).ToArray());

String.Concat的数组:

返回String.Concat(parent.Nodes()选择(X => x.ToString())的ToArray ());

我还没有表现出素旧的System.Xml算法在这里,因为它只是呼吁.InnerXml上的节点。


结论

如果性能是很重要的(如大量的XML,频频解析),我的使用丹尼尔的 CreateReader 方法每次。如果你只是做了几个疑问,您可能需要使用麦克更加简洁汇总的方法。

如果您正在使用大型XML元素有很多节点(也许100的),你可能会开始看到的StringBuilder 的使用效益上总结方法,但不超过 CreateReader 。我不认为加入 Concat的方法将永远是因为转换的惩罚这些条件更有效大名单大阵(甚至明显这里有较小的列表)。

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()
                };

解决方案

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 seconds)
  2. Plain old System.Xml - Greg Hurlman (0.134 seconds)
  3. Aggregate with string concatenation - Mike Powell (0.324 seconds)
  4. StringBuilder - Vin (0.333 seconds)
  5. String.Join on array - Terry (0.360 seconds)
  6. String.Concat on array - Marcin Kosieradzki (0.364)


Method

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>

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).

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

CreateReader:

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

Aggregate with string concatenation:

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 on array:

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

String.Concat on array:

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

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


Conclusion

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.

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天全站免登陆