Reading an XML file as a DataSet

I do not understand the parsing of XML files, and I save the line graph data in an XML file, so I did a little work. According to this , of all the ways to read an XML file, a DataSet is fast. And it makes sense that I use a DataSet , since there can be a significant amount of data. Here's what my graphic documents look like:

 <?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 can be a large number of dots in these lines, I need to get the data as quickly as possible and with minimal resources. If there is a faster approach than DataSet , please enlighten me. Otherwise, can someone show me how I retrieve my chart data using a DataSet as my XML parsing?

+7
source share
2 answers

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("\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 reads line by line. But this is a little trickier. You can see it here: http://msdn.microsoft.com/library/cc189056(v=vs.95).aspx

+10
source

Another simple method uses the built-in ReadXml method.

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

Note. The XML file must be ordered.

Link

+4
source

All Articles