由于 WPF 应用程序启动缓慢而替换 XmlSerializer
我编写了一个 WPF 应用程序,它从网络位置的 XML 文件读取电影列表。当我开始调查启动缓慢的情况时,发现 XmlSerializer 的开销很大。
我现在在发布项目之前使用 sgen 来构建程序集,但我现在正在寻找更好的解决方案。我查看了 BinaryFormatter 类,但 XML 文件是由 Linux 服务器上运行的 PHP 脚本创建的。
我是否最好使用 XML 文件阅读器并自己循环访问该文件,或者是否有更好的选择?我的目标是速度,因此欢迎任何替换我的 XmlSerializer 的建议。
这是反序列化文件的代码。
public List<Movie> DeSerializeXmlObject(string filename)
{
List<Movie> movies;
Stream stream = File.Open(filename, FileMode.Open);
XmlSerializer s = new XmlSerializer(typeof(List<Movie>));
movies = (List<Movie>)s.Deserialize(stream);
stream.Close();
return movies;
}
我不知道如何附加文件,因此我将 XML 文件粘贴到 Pastebin 上。 http://pastebin.com/Rxsy0R3c 谢谢 本
I have wrote a WPF application that reads a list of movies from a XML file at a network location. When I started investigating the slow start-up it turned out that XmlSerializer has a lot overhead.
I have now used sgen to build the assemblies before I publish the project but I am now looking into a better solution. I have looked at the BinaryFormatter class but the XML file is created by a PHP script running on the Linux server.
Would I be better to use an XML file reader and loop through the file myself or is there a better option? I am aiming for speed so any suggestions to replace my XmlSerializer are welcome.
Here is the code for de-serializing the the file.
public List<Movie> DeSerializeXmlObject(string filename)
{
List<Movie> movies;
Stream stream = File.Open(filename, FileMode.Open);
XmlSerializer s = new XmlSerializer(typeof(List<Movie>));
movies = (List<Movie>)s.Deserialize(stream);
stream.Close();
return movies;
}
I couldn't figure out how to attach files so I pasted the XML file onto pastebin. http://pastebin.com/Rxsy0R3c
Thanks
Ben
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您很有可能做错了什么。即使解析 1MB 的 xml 也不会超过 1 秒。您能否将您的序列化和反序列化代码与您尝试解析的 XML 文件一起发布?
编辑:抱歉,我想我帮不了你。您的代码和文件看起来没问题。我现在看到的唯一选择是在后台工作程序中延迟加载此文件,然后将电影设置回 GUI 线程中。
There is high possibility you are doing something wrong. Parsing even 1MB xml would take no more than 1 second. Would you kindly post your serialization and deserialization code with XML file you are trying to parse?
Edit: Sorry, I think I cant help you. Your code and file looks alright. Only option I see now is lazy load this file in background worker and then set movies back in GUI thread.