問題描述
我對解析 XML 文件沒有經驗,我正在將折線圖數據保存到 xml 文件中,所以我做了一些研究.根據 this文章,在所有讀取 XML 文件的方法中,DataSet
是最快的.我使用 DataSet
是有道理的,因為可能存在大量數據.這是我的圖表文檔的外觀:
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 this article, out of all the ways to read an XML file, DataSet
is the fastest. And it makes sense that I use DataSet
since there could be a significant amount of data. Here's how my graph documents look:
<?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 解析器來獲取我的圖形數據嗎?
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 DataSet
as my XML parser?
推薦答案
如果要使用DataSet,很簡單.
If you want to use a DataSet, it is very simple.
// 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(" " + 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(" " + row[i]);
}
Console.WriteLine();
}
}
如果您想要更快的速度,您可以嘗試使用 XmlReader 逐行讀取.但是開發難度有點大.你可以在這里看到它:http://msdn.microsoft.com/library/cc189056(v=vs.95).aspx
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
這篇關于將 XML 文件讀取為 DataSet的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!