读取大型xml文件并将其提取到c#中的表单应用程序中。网项目

时间:2022-09-15 10:44:24

I want to read a xml file data and extract information from it to show in a form in a C#.Net vs2008 env. I can't read this data into a list in Memory, but I want to have a .exe file from project that can run in another computer System. So I think, I can't use database for saving and retrieving data! Please help me to solve my problem!

我希望读取xml文件数据并从中提取信息,以c#形式显示。净vs2008 env。我无法将这些数据读到内存中的列表中,但我希望项目中的.exe文件可以在其他计算机系统中运行。所以我想,我不能使用数据库来保存和检索数据!请帮助我解决我的问题!

3 个解决方案

#1


3  

Use System.Xml.XmlReader to read the XML file. This is a forward only reader which only loads a bit of XML at a time.

使用System.Xml。读取XML文件的XmlReader。这是一个只能向前读取的阅读器,每次只加载一点XML。

Combine this with an iterator method which produces an IEnumerable, such as this example, where MyXmlData is a class representing what is held in the XML file and a class that your forms can work with:

结合迭代器方法,生成一个IEnumerable,例如这个示例,MyXmlData是表示XML文件中包含的内容的类,以及可以使用表单的类:

public IEnumerable<MyXmlData> QueryFile(String xmlFile)
{
    using (var reader = XmlReader.Create(xmlFile))
    {
        // These are the variables you want to read for each 'object' in the
        // xml file.
        var prop1 = String.Empty;
        var prop2 = 0;
        var prop3 = DateTime.Today;

        while (reader.Read())
        {
            // Here you'll have to read an xml node at a time.
            // As you read, assign appropriate values to the variables
            // declared above.

            if (/* Have we finished reading an item? */)
            {
                // Once you've completed reading xml representing a single
                // MyXmlData object, return it using yield return.
                yield return new MyXmlData(prop1, prop2, prop3);
            }
        }
    }
}

The value returned from that method is a sequence of MyXmlData objects where each one will be created on demand as the file is read, one at a time. This will greatly reduce memory requirements for a large XML file.

该方法返回的值是xmlmydata对象的序列,在每次读取文件时,将按需创建每个对象。这将大大减少对大型XML文件的内存需求。

You can then query your MyXmlData objects using Linq functions. For example, you can emulate paging by using the Take and Skip methods.

然后,您可以使用Linq函数查询您的MyXmlData对象。例如,可以通过使用Take和Skip方法来模拟分页。

// Third page - 50 objects per page.
QueryFile(@"x.xml").Skip(100).Take(50);

#2


0  

Recently microsoft provide a synidcation class in WCF. you can use it for doing this task

最近,微软在WCF中提供了一个synidcation类。你可以用它来做这个任务

#3


0  

You should look into VTD-XML, it is the most efficient xml parser in terms of memory usage without losing random access and xpath..

您应该研究VTD-XML,它是内存使用方面最有效的xml解析器,不会丢失随机访问和xpath。

http://vtd-xml.sf.net

http://vtd-xml.sf.net

#1


3  

Use System.Xml.XmlReader to read the XML file. This is a forward only reader which only loads a bit of XML at a time.

使用System.Xml。读取XML文件的XmlReader。这是一个只能向前读取的阅读器,每次只加载一点XML。

Combine this with an iterator method which produces an IEnumerable, such as this example, where MyXmlData is a class representing what is held in the XML file and a class that your forms can work with:

结合迭代器方法,生成一个IEnumerable,例如这个示例,MyXmlData是表示XML文件中包含的内容的类,以及可以使用表单的类:

public IEnumerable<MyXmlData> QueryFile(String xmlFile)
{
    using (var reader = XmlReader.Create(xmlFile))
    {
        // These are the variables you want to read for each 'object' in the
        // xml file.
        var prop1 = String.Empty;
        var prop2 = 0;
        var prop3 = DateTime.Today;

        while (reader.Read())
        {
            // Here you'll have to read an xml node at a time.
            // As you read, assign appropriate values to the variables
            // declared above.

            if (/* Have we finished reading an item? */)
            {
                // Once you've completed reading xml representing a single
                // MyXmlData object, return it using yield return.
                yield return new MyXmlData(prop1, prop2, prop3);
            }
        }
    }
}

The value returned from that method is a sequence of MyXmlData objects where each one will be created on demand as the file is read, one at a time. This will greatly reduce memory requirements for a large XML file.

该方法返回的值是xmlmydata对象的序列,在每次读取文件时,将按需创建每个对象。这将大大减少对大型XML文件的内存需求。

You can then query your MyXmlData objects using Linq functions. For example, you can emulate paging by using the Take and Skip methods.

然后,您可以使用Linq函数查询您的MyXmlData对象。例如,可以通过使用Take和Skip方法来模拟分页。

// Third page - 50 objects per page.
QueryFile(@"x.xml").Skip(100).Take(50);

#2


0  

Recently microsoft provide a synidcation class in WCF. you can use it for doing this task

最近,微软在WCF中提供了一个synidcation类。你可以用它来做这个任务

#3


0  

You should look into VTD-XML, it is the most efficient xml parser in terms of memory usage without losing random access and xpath..

您应该研究VTD-XML,它是内存使用方面最有效的xml解析器,不会丢失随机访问和xpath。

http://vtd-xml.sf.net

http://vtd-xml.sf.net