Read XML file as DataSet

后端 未结 2 1575
不知归路
不知归路 2020-12-30 08:14

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

相关标签:
2条回答
  • 2020-12-30 08:42

    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 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

    0 讨论(0)
  • 2020-12-30 09:00

    Other simple method is using "ReadXml" inbuilt method.

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

    Note: XML file should be orderly manner.

    Reference

    0 讨论(0)
提交回复
热议问题