WP7上XML性能优化的技巧 [英] Tips for XML performance optimization on WP7

查看:91
本文介绍了WP7上XML性能优化的技巧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在电话上有一个应用程序,它需要大约50页XML,每个XML大约有100个点头.因此,如果您进行的数学运算大约是5000个节点,那么我正在解析.有时,这些节点的设置不同.例如:也许75%的模式与其他25%的模式不同,所以有代码可以处理此模式并以不同的方式解析它们.

I have an application on the phone and it takes in about 50 pages of XML, each XML has about 100 nods in it. So if you do the math that is about 5000 nodes I am parsing. Sometimes these nodes are not set up the same. Example: maybe 75% have a different schema than the other 25% so there is code to handle this and parse them differently.

我再也无法优化http调用了,因为Web服务一次只能提供100个项目"的数据,因此,我必须打50次Web服务才能获得所有数据页.这是高级过程.

I can't optimize the http calls any more then I have as the web services only serve up data 100 "items" at a time, so I have to hit the web service 50 times basically to get all the pages of data. Here is the high level process.


Call web service (webclient)
Parse XML (take note total pages in xml. it will say Page 1 of 100)
Add results to collection 
Call web service again for page 2 
Parse
Add results to collection 
....rinse and repeat 100 times.

解析代码确实是我唯一可以优化的地方.我所要做的就是使用linq解析XML,并在IEnumerable中分离出节点,然后解析它们并将其放置在我创建的自定义对象中.我正在寻找有关如何优化整个过程的高级建议.也许我想念一些东西.

The parsing code is really the only place I can optimize. All I am doing is using linq to parse the XML and separate out the nodes in an IEnumerable and then I parse them and place them in a custom object I created. I'm looking for some high level ideas on how to optimize this entire process. Maybe I'm missing something.

一些代码....仅想象一下下面的内容,就像1000次或更多,并且具有更多的属性,这是一个小例子.大多数都具有30个需要解析的属性..此外,我也无法访问真实的架构,也无法控制架构变更.

Some code....just imagine the below, just like 1000 times or more, and with more attributes, this is a small example. Most have like 30 attributes that need parsing..Also I have no access to a real schema, and no control over schema changes.

XElement eventData = XElement.Parse(e.Result);
IEnumerable<XElement> feed =
    (eventData.Element("results").Elements("event").Select(el => el)).Distinct();
foreach (XElement el in feed)
{
    _brokenItem = el.ToString();
    thisFeeditem.InternalGuid = Guid.NewGuid().ToString();
    thisFeeditem.ServiceIcon = GetServiceIcon(thisFeeditem.ServiceType);
    thisFeeditem.Description = el.Attribute("displayName").Value;
    thisFeeditem.EventURL = el.Attribute("uri").Value;
    thisFeeditem.Guid = el.Attribute("id").Value;
    thisFeeditem.Latitude = el.Element("venue").Attribute("lat").Value;
    thisFeeditem.Longitude = el.Element("venue").Attribute("lng").Value;
}

推荐答案

没有看到您的代码,对其进行优化并不容易.但是,您应该考虑一个普遍点:

Without seeing your code, it is not easy to optimise it. However, there is one general point you should consider:

Linq-to-XML是基于DOM的解析器,因为它将整个XML文档读入驻留在内存中的模型中.所有查询均针对DOM执行.对于大型文档,构造DOM可能会占用大量内存和CPU.另外,如果您的Linq-to-XML查询编写效率低下,则可以多次浏览同一树节点.

Linq-to-XML is a DOM-based parser, in that it reads the entire XML document into a model which resides in memory. All queries are executed against the DOM. For large documents, constructing the DOM can be memory and CPU intensive. Also, your Linq-to-XML queries, if written inefficiently can navigate the same tree nodes multiple times.

作为替代方案,考虑使用串行解析器,例如 XmlReader .这种类型的解析器不会创建文档的基于内存的模型,而是以仅转发的方式运行,从而迫使您仅读取一次每个元素.

As an alternative, consider using a serial parser like XmlReader. Parsers of this type do not create a memory-based model of your document, and operate in a forward-only manner, forcing you to read each element just once.

这篇关于WP7上XML性能优化的技巧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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