Read XML file as DataSet

As a novice in parsing XML files, I needed to save line graph data to an xml file and conducted some research. Based on an article I found, among all the techniques for reading an XML file, this one is the quickest.


Question:

As I am new to
parsing XML files
, I had to save line
graph data
as an XML file. To find the best way to read the XML file, I did some research. Based on an article I found,

DataSet

is the quickest method out of all the options available. Considering the large amount of data, it is sensible to use

DataSet

. This is how my graph documents are presented:



    
        
            
            
        
        
            
                
                
                
                
            
            
                
                
                
                
            
        
    

As there might be a considerable amount of points in these lines, obtaining the data using minimal resources and in a timely manner is crucial. If there exists a quicker method than

DataSet

, kindly share it. Otherwise, can someone guide me on how to extract my graph data using an XML parser such as

DataSet

?



Solution 1:

Utilizing a DataSet is an effortless task.

// 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();
    }
}

For faster results, consider using XmlReader to read one line at a time. However, note that it may be more challenging to create. You can view more information here: http://msdn.microsoft.com/library/cc189056(v=vs.95).aspx


Solution 2:


Another uncomplicated approach involves utilizing the built-in “ReadXml” method.

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

Please ensure that the XML file is in the format of
orderly manner
.

Reference

Frequently Asked Questions

Posted in Uncategorized