C# 读取 XML 文件作为数据集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14412186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:43:55  来源:igfitidea点击:

Read XML file as DataSet

c#xmlperformancexml-parsingdataset

提问by Brandon Miller

I am inexperienced with parsing XML files, and I am saving line graph data to an xml file, so I did a little bit of research. According to thisarticle, out of all the ways to read an XML file, DataSetis the fastest. And it makes sense that I use DataSetsince there could be a significant amount of data. Here's how my graph documents look:

我对解析 XML 文件没有经验,我正在将折线图数据保存到一个 xml 文件中,所以我做了一些研究。根据这篇文章,在所有读取 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>

Since there could be a large number of points in these lines, I need to get the data as quickly and with as little resources as possible. If there is a faster approach than DataSet, please enlighten me. Otherwise, could someone show me how I would get my graph data using a DataSetas my XML parser?

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

采纳答案by jbouny

If you want to use a DataSet, it is very simple.

如果要使用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();
    }
}

If you want something faster, you can try with XmlReader which read line after line. But it is a bit more difficult to develop. You can see it here : http://msdn.microsoft.com/library/cc189056(v=vs.95).aspx

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

回答by Rock star

Other simple method is using "ReadXml" inbuilt method.

其他简单的方法是使用“ReadXml”内置方法。

string filePath = "D:\Self Practice\Sol1\Sol1\Information.xml";
DataSet ds = new DataSet();
ds.ReadXml(filePath);

Note: XML file should be orderly manner.

注意:XML 文件应该是有序的。

Reference

参考