需求的XDocument和负载过大的XML文件 [英] XDocument and load too large XML file on demand

查看:150
本文介绍了需求的XDocument和负载过大的XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有写这篇code读取XML文件的网址:

I did write this code to read a url of xml file:

XDocument feedXml = XDocument.Load("url address of xml file here");
        var feeds = from feed in feedXml.Descendants("List")
                    select new Event { 
                        Id = Int32.Parse(feed.Element("ID").Value),
                        Name = feed.Element("Name").Value,                            
                        City = feed.Element("City").Value                            
                    };
        return feeds;

我的问题是,该文件过大(约40MB),并花太多时间来加载。
所以我使用的XmlReader读取XML文件,但此并不适用,因为太大,我不知道该怎么对每个(例如10)记录在每一个需求的页面加载,我应该读整个文件每次和跳过其他记录达到适当的元素,应该不是我?

My problem is that the file is too large (about 40MB) and take too much time to load. So I am using XmlReader to read xml file but this was not applicable too because I don't know how to load every (for example 10) records in every page on demand and I should read the whole file every time and skip other records to reach appropriate elements, shouldn't I?

string XmlFileUrl = @"url address of xml file here";
        using (XmlReader reader = new XmlTextReader(XmlFileUrl))
        {     
            bool openItem = false;
            Event item = new Event();

            while (reader.Read())
            {                    
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == "List")
                    {
                        item = new Event();
                        openItem = true;
                    }
                    else if (reader.Name == "Name" && openItem)
                        item.Name = reader.ReadElementContentAsString();
                        ...
                }
                else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "List" && openItem)
                {
                    openItem = false;
                    feeds.Add(item);                                     
                }
            }
        }

有没有办法使用jQuery AJAX或转换XML文件分页JSON加载每个页面上只需要数据,或任何建议?

Is there any way to use Jquery ajax or convert the xml file to json with paging to load just needed data on every page, or any suggestion?

推荐答案

我觉得这不是那么容易实现的一个XML结构,也许在这种情况下是 XDocument.Load 不是合适的方法,因为据我所知它总是立即加载整个文档。您可以尝试超载一>,而不是URL,并尝试加载只有文件过网的一部分。也许你需要编写自己的装载机(即仅获取文件,可能的 XmlDocument的?),并自行解析不完整的结构。

I think this is not so easy to achieve with a XML structure and maybe in this case is XDocument.Load not the appropriate method, because AFAIK it always loads the whole document immediately. You can try the overload with the Stream parameter, instead of URL, and try loading only a part of the the file over the net. Probably you need to write your own loader (that gets only a portion of the file, maybe XmlDocument?) and parse the incomplete structure by yourself.

如果你可以调用由URI控制的XML文件的某些部分(例如:的http://域/项页= 10安培;取= 20 这调用返回一个有效的XML),那么一个选项是使用这个网址,而不是链接到整个文件,是这样的:

If you can call portions of the XML file controlled by the URI (for example: http://domain/entries?page=10&take=20 and this call returns a valid XML), then an option would be to use this URL instead of the link to the whole file, something like:

var pagedUri = @"http://domain/entries?page=10&take=20";
XDocument feedXml = XDocument.Load(pagedUri);
var feeds = from feed in feedXml.Descendants("List")
            select new Event {
                Id = Int32.Parse(feed.Element("ID").Value),
                Name = feed.Element("Name").Value,
                City = feed.Element("City").Value
            };
return feeds;

看一看这个 SO帖子哪里有类似的问题得到解决

Have a look at this SO post where a similar problem is addressed.

这篇关于需求的XDocument和负载过大的XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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