0

I am trying to get the currencycode from following xml file. But I get an exception called IndexOutOfRange.

<string xmlns="http://www.webserviceX.NET">
<NewDataSet> 
<Table> 
    <Name>India</Name> 
    <CountryCode>in</CountryCode> 
    <Currency>Rupee</Currency> 
    <CurrencyCode>INR</CurrencyCode> 
</Table> 
<Table> 
    <Name>India</Name> 
    <CountryCode>in</CountryCode> 
    <Currency>Rupee</Currency> 
    <CurrencyCode>INR</CurrencyCode>
</Table> 
</NewDataSet>
</string>

Here is my code,

string firstCurrency = countryService.GetCurrencyByCountry(this.DropDownList1.SelectedIndex.ToString());
DataSet dataset = new DataSet();
dataset.ReadXml(new XmlTextReader(new StringReader(firstCurrency)));
String currency = dataset.Tables[0].Rows[0][2].ToString();
String currencyCode = dataset.Tables[0].Rows[0][3].ToString();
currencyLabel.Text = currency;
currencyCodeLabel.Text = currencyCode;

Error occurs at the following line,

String currency = dataset.Tables[0].Rows[0][2].ToString();

I tried several hours, but could not find a solution.

Isuru
  • 3,818
  • 13
  • 49
  • 64
  • 1
    Look at your dataset with debugger. It shouldn't take several hours. I wonder how people are getting so far with without knowing how to use it.. – Eugene Sh. Dec 23 '14 at 14:50
  • 3
    Well have you checked that there *is* a table, and that it *has* a first row, and that that row has 3 columns? You're doing three indexing operations in a single statement - have you tried to find out which of them has failed? – Jon Skeet Dec 23 '14 at 14:50
  • 1
    I suggest stepping into your code and set a breakpoint on the line throwing the error and use the debugger to explore your dataset object. – Chris Walsh Dec 23 '14 at 14:51
  • I'm guessing your first line (GetCurrencyByCountry) isn't actually returning the string you're expecting. – erstaples Dec 23 '14 at 14:52

2 Answers2

0

You can try this:

String currency = dataset.Tables["Table"].Rows[0][2].ToString()

Your XML is giving you what you don't expect. It creates two tables. One called "NewDataSet" and the other called "Table". You will find your rows in your other table (Tables[1]). So, fix your dataset xml or use code I gave you above.

Btw, I would never advise such approach when obtaining values from datatable. At least check if values are null before accessing them via index.

buhtla
  • 2,819
  • 4
  • 25
  • 38
  • 1
    Strange. I used your exact XML as posted in your original question, created test app, loaded xml isnde dataset, and with the line of code above I accessed the value without a problem. If your XML is not same as you originally posted, than you will have to check for nulls, as I stated in my original answer. – buhtla Dec 23 '14 at 17:48
0

As noted by @buthla, it does not give desired XML.

In order to have expected xml you should update your xml as:

<string xmlns="http://www.webserviceX.NET">
<Table> 
    <Name>India</Name> 
    <CountryCode>in</CountryCode> 
    <Currency>Rupee</Currency> 
    <CurrencyCode>INR</CurrencyCode> 
</Table> 
<Table> 
    <Name>India</Name> 
    <CountryCode>in</CountryCode> 
    <Currency>Rupee</Currency> 
    <CurrencyCode>INR</CurrencyCode>
</Table> 
</string>

You may want to have a look at this post as well

In order to inspect your dataset in debugger, follow the screen shots

enter image description here

enter image description here

Community
  • 1
  • 1
asdf_enel_hak
  • 7,474
  • 5
  • 42
  • 84