xml reader in C#.

Not sure how to do this. Do I build the schema file and use that?
I get an error.  
System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 44:             ds.ReadXmlSchema(Server.MapPath("GeneralInformation/Countries.xsd"));
Line 45:
Line 46:             DataRow[] drs = ds.Tables["countries"].Select("countryid = '" + ID + "'");
Line 47:             if (drs.Length != 0)
Line 48:             {

public string GetCountryName(string ID)
            DataSet ds = new DataSet();

            DataRow[] drs = ds.Tables["countries"].Select("countryid = '" + ID + "'");
            if (drs.Length != 0)
                return drs[0]["name"].ToString();
                return String.Empty;

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
  <country countryid="AF" name="Afghanistan" />
  <country countryid="AL" name="Albania" />
  <country countryid="DZ" name="Algeria" />
  <country countryid="AS" name="American Samoa" />
  <country countryid="AD" name="Andorra" />
  <country countryid="AO" name="Angola" />
  <country countryid="AI" name="Anguilla" />
  <country countryid="AQ" name="Antarctica" />
  <country countryid="AG" name="Antigua And Barbuda" />
  <country countryid="AR" name="Argentina" />
  <country countryid="AM" name="Armenia" />
  <country countryid="AW" name="Aruba" />
  <country countryid="AU" name="Australia" />
  <country countryid="AT" name="Austria" />
  <country countryid="AZ" name="Azerbaijan" />
  <country countryid="BS" name="Bahamas" />
  <country countryid="BH" name="Bahrain" />
  <country countryid="BD" name="Bangladesh" />
  <country countryid="BB" name="Barbados" />
  <country countryid="BY" name="Belarus" />
  <country countryid="BE" name="Belgium" />
  <country countryid="BZ" name="Belize" />
  <country countryid="BJ" name="Benin" />
  <country countryid="BM" name="Bermuda" />
  <country countryid="BT" name="Bhutan" />
  <country countryid="BO" name="Bolivia" />
  <country countryid="BA" name="Bosnia And Herzegowina" />
  <country countryid="BW" name="Botswana" />
  <country countryid="BV" name="Bouvet Island" />
  <country countryid="BR" name="Brazil" />
  <country countryid="IO" name="British Indian Ocean Territory" />
  <country countryid="BN" name="Brunei Darussalam" />
  <country countryid="BG" name="Bulgaria" />
  <country countryid="BF" name="Burkina Faso" />
  <country countryid="BI" name="Burundi" />
  <country countryid="KH" name="Cambodia" />
  <country countryid="CM" name="Cameroon" />
  <country countryid="CA" name="Canada" />
  <country countryid="CV" name="Cape Verde" />
  <country countryid="KY" name="Cayman Islands" />
  <country countryid="CF" name="Central African Republic" />
  <country countryid="TD" name="Chad" />
  <country countryid="CL" name="Chile" />
  <country countryid="CN" name="China" />
  <country countryid="CX" name="Christmas Island" />
  <country countryid="CC" name="Cocos (Keeling) Islands" />
  <country countryid="CO" name="Colombia" />
  <country countryid="KM" name="Comoros" />
  <country countryid="CG" name="Congo" />
  <country countryid="CK" name="Cook Islands" />
  <country countryid="CR" name="Costa Rica" />
  <country countryid="CI" name="Cote D'Ivoire" />
  <country countryid="HR" name="Croatia (Local Name: Hrvatska)" />
  <country countryid="CU" name="Cuba" />
  <country countryid="CY" name="Cyprus" />
  <country countryid="CZ" name="Czech Republic" />
  <country countryid="DK" name="Denmark" />
  <country countryid="DJ" name="Djibouti" />
  <country countryid="DM" name="Dominica" />
  <country countryid="DO" name="Dominican Republic" />
  <country countryid="TP" name="East Timor" />
  <country countryid="EC" name="Ecuador" />
  <country countryid="EG" name="Egypt" />
  <country countryid="SV" name="El Salvador" />
  <country countryid="GQ" name="Equatorial Guinea" />
  <country countryid="ER" name="Eritrea" />
  <country countryid="EE" name="Estonia" />
  <country countryid="ET" name="Ethiopia" />
  <country countryid="FK" name="Falkland Islands (Malvinas)" />
  <country countryid="FO" name="Faroe Islands" />
  <country countryid="FJ" name="Fiji" />
  <country countryid="FI" name="Finland" />
  <country countryid="FR" name="France" />
  <country countryid="GF" name="French Guiana" />
  <country countryid="PF" name="French Polynesia" />
  <country countryid="TF" name="French Southern Territories" />
  <country countryid="GA" name="Gabon" />
  <country countryid="GM" name="Gambia" />
  <country countryid="GE" name="Georgia" />
  <country countryid="DE" name="Germany" />
  <country countryid="GH" name="Ghana" />
  <country countryid="GI" name="Gibraltar" />
  <country countryid="GR" name="Greece" />
  <country countryid="GL" name="Greenland" />
  <country countryid="GD" name="Grenada" />
  <country countryid="GP" name="Guadeloupe" />
  <country countryid="GU" name="Guam" />
  <country countryid="GT" name="Guatemala" />
  <country countryid="GN" name="Guinea" />
  <country countryid="GW" name="Guinea-Bissau" />
  <country countryid="GY" name="Guyana" />
  <country countryid="HT" name="Haiti" />
  <country countryid="HM" name="Heard And Mc Donald Islands" />
  <country countryid="VA" name="Holy See (Vatican City State)" />
  <country countryid="HN" name="Honduras" />
  <country countryid="HK" name="Hong Kong" />
  <country countryid="HU" name="Hungary" />
  <country countryid="IS" name="Icel And" />
  <country countryid="IN" name="India" />
  <country countryid="ID" name="Indonesia" />
  <country countryid="IR" name="Iran (Islamic Republic Of)" />
  <country countryid="IQ" name="Iraq" />
  <country countryid="IE" name="Ireland" />
  <country countryid="IL" name="Israel" />
  <country countryid="IT" name="Italy" />
  <country countryid="JM" name="Jamaica" />
  <country countryid="JP" name="Japan" />
  <country countryid="JO" name="Jordan" />
  <country countryid="KZ" name="Kazakhstan" />
  <country countryid="KE" name="Kenya" />
  <country countryid="KI" name="Kiribati" />
  <country countryid="KP" name="Korea, Dem People'S Republic" />
  <country countryid="KR" name="Korea, Republic Of" />
  <country countryid="KW" name="Kuwait" />
  <country countryid="KG" name="Kyrgyzstan" />
  <country countryid="LA" name="Lao People'S Dem Republic" />
  <country countryid="LV" name="Latvia" />
  <country countryid="LB" name="Lebanon" />
  <country countryid="LS" name="Lesotho" />
  <country countryid="LR" name="Liberia" />
  <country countryid="LY" name="Libyan Arab Jamahiriya" />
  <country countryid="LI" name="Liechtenstein" />
  <country countryid="LT" name="Lithuania" />
  <country countryid="LU" name="Luxembourg" />
  <country countryid="MO" name="Macau" />
  <country countryid="MK" name="Macedonia" />
  <country countryid="MG" name="Madagascar" />
  <country countryid="MW" name="Malawi" />
  <country countryid="MY" name="Malaysia" />
  <country countryid="MV" name="Maldives" />
  <country countryid="ML" name="Mali" />
  <country countryid="MT" name="Malta" />
  <country countryid="MH" name="Marshall Islands" />
  <country countryid="MQ" name="Martinique" />
  <country countryid="MR" name="Mauritania" />
  <country countryid="MU" name="Mauritius" />
  <country countryid="YT" name="Mayotte" />
  <country countryid="MX" name="Mexico" />
  <country countryid="FM" name="Micronesia, Federated States" />
  <country countryid="MD" name="Moldova, Republic Of" />
  <country countryid="MC" name="Monaco" />
  <country countryid="MN" name="Mongolia" />
  <country countryid="MS" name="Montserrat" />
  <country countryid="MA" name="Morocco" />
  <country countryid="MZ" name="Mozambique" />
  <country countryid="MM" name="Myanmar" />
  <country countryid="NA" name="Namibia" />
  <country countryid="NR" name="Nauru" />
  <country countryid="NP" name="Nepal" />
  <country countryid="NL" name="Netherlands" />
  <country countryid="AN" name="Netherlands Ant Illes" />
  <country countryid="NC" name="New Caledonia" />
  <country countryid="NZ" name="New Zealand" />
  <country countryid="NI" name="Nicaragua" />
  <country countryid="NE" name="Niger" />
  <country countryid="NG" name="Nigeria" />
  <country countryid="NU" name="Niue" />
  <country countryid="NF" name="Norfolk Island" />
  <country countryid="MP" name="Northern Mariana Islands" />
  <country countryid="NO" name="Norway" />
  <country countryid="OM" name="Oman" />
  <country countryid="PK" name="Pakistan" />
  <country countryid="PW" name="Palau" />
  <country countryid="PA" name="Panama" />
  <country countryid="PG" name="Papua New Guinea" />
  <country countryid="PY" name="Paraguay" />
  <country countryid="PE" name="Peru" />
  <country countryid="PH" name="Philippines" />
  <country countryid="PN" name="Pitcairn" />
  <country countryid="PL" name="Poland" />
  <country countryid="PT" name="Portugal" />
  <country countryid="PR" name="Puerto Rico" />
  <country countryid="QA" name="Qatar" />
  <country countryid="RE" name="Reunion" />
  <country countryid="RO" name="Romania" />
  <country countryid="RU" name="Russian Federation" />
  <country countryid="RW" name="Rwanda" />
  <country countryid="KN" name="Saint K Itts And Nevis" />
  <country countryid="LC" name="Saint Lucia" />
  <country countryid="VC" name="Saint Vincent, The Grenadines" />
  <country countryid="WS" name="Samoa" />
  <country countryid="SM" name="San Marino" />
  <country countryid="ST" name="Sao Tome And Principe" />
  <country countryid="SA" name="Saudi Arabia" />
  <country countryid="SN" name="Senegal" />
  <country countryid="SC" name="Seychelles" />
  <country countryid="SL" name="Sierra Leone" />
  <country countryid="SG" name="Singapore" />
  <country countryid="SK" name="Slovakia (Slovak Republic)" />
  <country countryid="SI" name="Slovenia" />
  <country countryid="SB" name="Solomon Islands" />
  <country countryid="SO" name="Somalia" />
  <country countryid="ZA" name="South Africa" />
  <country countryid="GS" name="South Georgia , S Sandwich Is." />
  <country countryid="ES" name="Spain" />
  <country countryid="LK" name="Sri Lanka" />
  <country countryid="SH" name="St. Helena" />
  <country countryid="PM" name="St. Pierre And Miquelon" />
  <country countryid="SD" name="Sudan" />
  <country countryid="SR" name="Suriname" />
  <country countryid="SJ" name="Svalbard, Jan Mayen Islands" />
  <country countryid="SZ" name="Sw Aziland" />
  <country countryid="SE" name="Sweden" />
  <country countryid="CH" name="Switzerland" />
  <country countryid="SY" name="Syrian Arab Republic" />
  <country countryid="TW" name="Taiwan" />
  <country countryid="TJ" name="Tajikistan" />
  <country countryid="TZ" name="Tanzania, United Republic Of" />
  <country countryid="TH" name="Thailand" />
  <country countryid="TG" name="Togo" />
  <country countryid="TK" name="Tokelau" />
  <country countryid="TO" name="Tonga" />
  <country countryid="TT" name="Trinidad And Tobago" />
  <country countryid="TN" name="Tunisia" />
  <country countryid="TR" name="Turkey" />
  <country countryid="TM" name="Turkmenistan" />
  <country countryid="TC" name="Turks And Caicos Islands" />
  <country countryid="TV" name="Tuvalu" />
  <country countryid="UG" name="Uganda" />
  <country countryid="UA" name="Ukraine" />
  <country countryid="AE" name="United Arab Emirates" />
  <country countryid="GB" name="United Kingdom" />
  <country countryid="US" name="United States" />
  <country countryid="UM" name="United States Minor Is." />
  <country countryid="UY" name="Uruguay" />
  <country countryid="UZ" name="Uzbekistan" />
  <country countryid="VU" name="Vanuatu" />
  <country countryid="VE" name="Venezuela" />
  <country countryid="VN" name="Viet Nam" />
  <country countryid="VG" name="Virgin Islands (British)" />
  <country countryid="VI" name="Virgin Islands (U.S.)" />
  <country countryid="WF" name="Wallis And Futuna Islands" />
  <country countryid="EH" name="Western Sahara" />
  <country countryid="YE" name="Yemen" />
  <country countryid="YU" name="Yugoslavia" />
  <country countryid="ZR" name="Zaire" />
  <country countryid="ZM" name="Zambia" />
  <country countryid="ZW" name="Zimbabwe" />

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sachintana DissanayakeSenior Web DeveloperCommented:
You are not reading an xml schema, its a XML file. so you have to use ds.ReadXml() method instead of ds.ReadXmlSchema().

And other thing is you are reading a non-existing table from the table collection in the dataset.
So it also has to change to ds.Tables["country"], since your xml root element of each country element is "country".

Here is the corrected code...

DataSet ds = new DataSet();
DataRow[] drs = ds.Tables["country"].Select("countryid = '" + ID + "'");
if (drs.Length != 0)
    return drs[0]["name"].ToString();
    return String.Empty;

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Hi mathieu_cupryk;

When I ask you to post a sample of the file I did not mean for you to open a new question just to put this info on the original post.

Fernando SotoRetiredCommented:
Hi mathieu_cupryk;

Here is the code corrected to work with the file Countries.xml and not xsd.

The table name that the ds.ReadXml method used was country and not countries.

public string GetCountryName(string ID)
    DataSet ds = new DataSet();
    DataRow[] drs = ds.Tables["country"].Select("countryid = '" + ID + "'");
    if (drs.Length != 0)
        return drs[0]["name"].ToString();
        return String.Empty;

Open in new window

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Fernando SotoRetiredCommented:
Hi mathieu_cupryk;

Error in my last post in the code, this line of code:
Should be:

Use an xml file not a xsd file

mathieu_cuprykAuthor Commented:
Good job guys.
mathieu_cuprykAuthor Commented:
it's all good.
Sachintana DissanayakeSenior Web DeveloperCommented:
I am sorry FernandoSoto, I didn't notice that its a question carried by you.
Any all got sorted, cheers...
Fernando SotoRetiredCommented:
Hi sachintana;

That's not a problem. The questions are always open to all to join in on and help the author of the question.

Have a great day;
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.