Brandon Miller Brandon Miller - 1 year ago 61
C# Question

Read XML file as 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,

is the fastest. And it makes sense that I use
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" ?>
<Graph Version="3.0" Mode="static">
<X Min="-20" Max="20" />
<Y Min="-20" Max="20" />
<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 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" />

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
, please enlighten me. Otherwise, could someone show me how I would get my graph data using a
as my XML parser?

Answer Source

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)
    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)));
    foreach (var row in table.AsEnumerable())
        for (int i = 0; i < table.Columns.Count; ++i)
            Console.Write("\t" + row[i]);

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 :

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download