c# - 读取 XML 文件作为数据集

标签 c# xml performance xml-parsing dataset

我对解析 XML 文件没有经验,我正在将折线图数据保存到 xml 文件中,所以我做了一些研究。根据this文章中,在读取 XML 文件的所有方法中,DataSet 是最快的。我使用 DataSet 是有道理的,因为可能有大量数据。这是我的图表文档的样子:

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<BreezyCalc>
    <Graph Version="3.0" Mode="static">
        <Range>
            <X Min="-20" Max="20" />
            <Y Min="-20" Max="20" />
        </Range>
        <Lines>
            <Line Name="MyLine1" R="0" G="255" B="0">
                <Point X="-17" Y="9" />
                <Point X="7" Y="-5" />
                <Point X="10" Y="4" />
                <Point X="-6" Y="2" />
            </Line>
            <Line Name="MyLine2" R="255" G="0" B="0">
                <Point X="-7" Y="3" />
                <Point X="8" Y="-1" />
                <Point X="-4" Y="-4" />
                <Point X="-1" Y="6" />
            </Line>
        </Lines>
    </Graph>
</BreezyCalc>

由于这些线中可能有大量点,因此我需要尽可能快地获取数据并使用尽可能少的资源。如果有比DataSet更快的方法,请赐教。否则,有人可以告诉我如何使用 DataSet 作为我的 XML 解析器来获取我的图形数据吗?

最佳答案

如果要使用DataSet,很简单。

// Here your xml file
string xmlFile = "Data.xml";

DataSet dataSet = new DataSet();
dataSet.ReadXml(xmlFile, XmlReadMode.InferSchema);

// Then display informations to test
foreach (DataTable table in dataSet.Tables)
{
    Console.WriteLine(table);
    for (int i = 0; i < table.Columns.Count; ++i)
        Console.Write("\t" + table.Columns[i].ColumnName.Substring(0, Math.Min(6, table.Columns[i].ColumnName.Length)));
    Console.WriteLine();
    foreach (var row in table.AsEnumerable())
    {
        for (int i = 0; i < table.Columns.Count; ++i)
        {
            Console.Write("\t" + row[i]);
        }
        Console.WriteLine();
    }
}

如果您想要更快的速度,可以尝试使用逐行读取的 XmlReader。但是开发起来有点困难。 你可以在这里看到它:http://msdn.microsoft.com/library/cc189056(v=vs.95).aspx

关于c# - 读取 XML 文件作为数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14412186/

相关文章:

c# - savechanges 方法中的“System.Data.Entity.Infrastructure.DbUpdateException”

c# - 未能发布 Windows 窗体项目

c# - 当我在 WPF Mvvm 中按下按钮时,如何在 DataGrid 中显示数据?

java - 尝试在java中将xslt应用于xml

c# - 进入/调试与客户端不同的解决方案中的 WCF 服务

ios - 使用 xml 在 iOS 5 应用程序上创建文件,将数据导出到 *.xls 文件

java - Spring框架连接多个数据库xml JAVA

javascript - 这些选择器中哪一个可能更快?

在 WAN 上具有许多绑定(bind)变量的 oracle 插入非常慢

database - 以低成本方式托管大型表并保持性能可扩展?