0

I have started with a simple DataTable, in which I have written to a XML file. On the next time the program runs, I want to check to see if the XML file exits, and read it.

It appears writing the xml is fine, and I believe reading it is fine, but I cannot seem to get any of the data within the DataSet after I have read from it..

ds.WriteXml(@"C:\Computers\config.xml");
if (File.Exists(@"C:\Computers\config.xml"))
{
    ds.ReadXml(@"C:\Computers\config.xml");
    //comboBox.Items.Add(ds.Tables[0].Rows[0][0].ToString()); doesn't work
    comboBox.Items.Add(ds.Tables[0].Rows.Count);  //this counts 3 rows
}

I receive this error.

An unhandled exception of type 'System.IndexOutOfRangeException' occurred in System.Data.dll

Additional information: Cannot find column 1.

here's a look at my XML file

 <?xml version="1.0" standalone="yes"?>
<NewDataSet>
    <Table1>
        <Name>Test1</Name>
        <Version>1.1.1.1</Version>
        <Code />
        <Location>C:\Computers\test.txt</Location>
    </Table1>
    <Table1>
        <Name>test2</Name>
        <Version />
        <Code />
        <Location />
    </Table1>
    <Table1>
        <Name>test3</Name>
        <Version />
        <Code />
        <Location />
    </Table1>
</NewDataSet>        
      

I'm just trying to get the "Name" Field from each row, what am I doing wrong?

Community
  • 1
  • 1
Rickybobby
  • 245
  • 1
  • 13

3 Answers3

0

I put your XML into C:\computer And run the code

DataSet ds =new DataSet();

if (File.Exists(@"C:\Computers\config.xml"))
{
            ds.ReadXml(@"C:\Computers\config.xml");

            //comboBox.Items.Add(ds.Tables[0].Rows[0][0].ToString()); doesn't work
            comboBox.Items.Add(ds.Tables[0].Rows.Count);  //this counts 3 rows
}

comboBox.Items.Add(ds.Tables[0].Rows[0][0].ToString() is Tesy1 ds.Tables[0].Rows.Count is 3

so, I think problem is not here.

MichaelMao
  • 2,596
  • 2
  • 23
  • 54
  • Right, the count works fine, but if I use the commented out code I cannot seem to get the actual value "Test1" "Test2" or "Test3" – Rickybobby Nov 13 '15 at 03:36
  • Figured it out, It was the scope of my Dataset, for whatever reason I put it at the top within my main window. Changing the scope of the object resolved my issue. Not exactly sure why it didn't like being where it was, but whatever. – Rickybobby Nov 13 '15 at 03:59
0

The problem is when you write

ds.WriteXml(@"C:\Computers\config.xml");

in the first line it would write a new file and thus when you read there is no data inside it. So now you should remove the first line and then check it should be running fine

if (File.Exists(@"C:\Computers\config.xml"))
{
    ds.ReadXml(@"C:\Computers\config.xml");
    comboBox.Items.Add(ds.Tables[0].Rows[0][0].ToString()); //doesn't work
    comboBox.Items.Add(ds.Tables[0].Rows.Count);  //this counts 3 rows
}
Mohit S
  • 13,723
  • 6
  • 34
  • 69
  • ds.WriteXml(@"C:\Computers\config.xml"); is only in the Window closed event, I just included that above so people knew I was writing the file the that specific path – Rickybobby Nov 13 '15 at 03:39
  • Try to debug. Put a break point on ReadXML and when your cursor reaches there go and check the file. Does it have something to read. I am sure you will get the clue ... bcoz code is running fine. – Mohit S Nov 13 '15 at 03:43
  • is it possible my DataTable structure is incorrect? I posted what it looks like below – Rickybobby Nov 13 '15 at 03:49
0

Is it possible the DataTable isn't structured correctly?

            public DataTable predefinedPatch = new DataTable();

            predefinedPatch.Columns.Add("Name");
            predefinedPatch.Columns.Add("Version");
            predefinedPatch.Columns.Add("Code");
            predefinedPatch.Columns.Add("Location");
            //
            predefinedPatch.Rows.Add("test1", "", "", "");
            predefinedPatch.Rows.Add("test2", "", "", "");
            predefinedPatch.Rows.Add("test3", "", "", "");
Rickybobby
  • 245
  • 1
  • 13
  • Since you are new to SO. You can always edit your question instead of putting it as answer. – Mohit S Nov 13 '15 at 03:50
  • I believe when you are writing it into XML file you can see if it is structured in the way you have shown in the question. If so than it is correct. If not let us know what is your XML looks like – Mohit S Nov 13 '15 at 03:52
  • Got it, thanks. It was the scope of the object, I had it as a public dataset at the top within the MainWindow, it didn't have a problem with writing the xml, but I guess it has issues reading it. – Rickybobby Nov 13 '15 at 04:00