?
Solved

xml reader in C#.

Posted on 2008-02-09
8
Medium Priority
?
1,450 Views
Last Modified: 2013-12-17
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();
            ds.ReadXmlSchema(Server.MapPath("GeneralInformation/Countries.xsd"));

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

        }
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<countries>
  <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" />
 
</countries>

Open in new window

0
Comment
Question by:mathieu_cupryk
  • 4
  • 2
  • 2
8 Comments
 
LVL 8

Accepted Solution

by:
Sachintana Dissanayake earned 1000 total points
ID: 20860212
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();
ds.ReadXml(Server.MapPath("GeneralInformation/Countries.xsd"));
 
DataRow[] drs = ds.Tables["country"].Select("countryid = '" + ID + "'");
if (drs.Length != 0)
{
    return drs[0]["name"].ToString();
}
else
{
    return String.Empty;
}

Open in new window

0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 20861520
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
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 20861551
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.

Fernando
public string GetCountryName(string ID)
{
    DataSet ds = new DataSet();
    ds.ReadXml(Server.MapPath("GeneralInformation/Countries.xsd"));
    DataRow[] drs = ds.Tables["country"].Select("countryid = '" + ID + "'");
    if (drs.Length != 0)
    {
        return drs[0]["name"].ToString();
    }
    else
    {
        return String.Empty;
    }
}

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 1000 total points
ID: 20861583
Hi mathieu_cupryk;

Error in my last post in the code, this line of code:
    ds.ReadXml(Server.MapPath("GeneralInformation/Countries.xsd"));
Should be:
    ds.ReadXml(Server.MapPath("GeneralInformation/Countries.xml"));

Use an xml file not a xsd file

Fernando
   
0
 

Author Closing Comment

by:mathieu_cupryk
ID: 31429581
Good job guys.
0
 

Author Comment

by:mathieu_cupryk
ID: 20865995
it's all good.
0
 
LVL 8

Expert Comment

by:Sachintana Dissanayake
ID: 20868443
I am sorry FernandoSoto, I didn't notice that its a question carried by you.
Any all got sorted, cheers...
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 20869108
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;
Fernando
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Native ability to set a user account password via AD GPO was removed because the passwords can be easily decrypted by any authenticated user in the domain. Microsoft recommends LAPS as a replacement and I have written an article that does something …
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
Suggested Courses
Course of the Month7 days, 8 hours left to enroll

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question