0

i am trying to read an xml file and a txt file using one button click i currently have this code in my button, i don't know what it is that i'm doing wrong any help or advice of sort would be great.

        try
        {
            XmlReader file;
            file = XmlReader.Create("c:/CSAIO4D/BK01/CH01/ReadFiles/ReadFiles/XMLFile1.xml", new XmlReaderSettings());

            DataSet ds = new DataSet();

            ds.ReadXml(file);

            dataGridView1.DataSource = ds.Tables[0];



            StreamReader files = new StreamReader("c:/CSAIO4D/BK01/CH01/ReadFiles/ReadFiles/People.txt");
            string[] columnnames = files.ReadLine().Split(',');

            DataTable dt = new DataTable();
            foreach (string c in columnnames)
            {
                dt.Columns.Add(c);
            }
            string newline;
            while ((newline = files.ReadLine()) != null)
            {
                DataRow dr = dt.NewRow();
                string[] values = newline.Split(',');
                for (int i = 0; i < values.Length; i++)
                {
                    dr[i] = values[i];
                }
                dt.Rows.Add(dr);
            }
            file.Close();

            dataGridView1.DataSource = dt;

            dataGridView1.Visible = true;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

can someone please help me out, i am new to web forms, the issue that i am having is that it only reads the text file and return it to the DataGridView and does not read the xml

  • Are you getting an exception? What is going wrong? – Flocke Jul 03 '18 at 07:51
  • You need to work on your question. What is the problem you are experiencing? – Pieter Alberts Jul 03 '18 at 07:51
  • You cannot have two different data sources on the same grid. You have `dataGridView1.DataSource = ds.Tables[0];`, then later `dataGridView1.DataSource = dt;`. The later one will override the first one... Do you have two separate grids, one for each source? Is this a copy/paste error? – user1429080 Jul 03 '18 at 12:34

1 Answers1

0

Here is How to read XML File

XmlDocument doc = new XmlDocument();
doc.Load("c:\\temp.xml");

Here is how to read XML in the dataset

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

And here is how you can read a text file

 string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Public\TestFolder\WriteLines2.txt");

        // Display the file contents by using a foreach loop.
        System.Console.WriteLine("Contents of WriteLines2.txt = ");
Mihir Dave
  • 3,954
  • 1
  • 12
  • 28