Reading states or provinces xml file in C#

I have to read an id for either a state or a province. The id must check against the two xml files attached.
I have this:
                                                    City                                                 State/Province
 lblLocation.Text =  reader.GetString(22).ToString() + ", "  GetProvincesorStatesName(reader.GetString(20)) + GetCountryName(reader.GetString(19));
                       Country.


GetProvincesorStatesName(reader.GetString(20))

I must modify the below procedure to get a province or state name or empty?
 public string GetProvincesorStatesName(string ID)
        {
            DataSet ds = new DataSet();

            ds.ReadXml(Server.MapPath("GeneralInformation/States.xml"));
            ds.ReadXml(Server.MapPath("GeneralInformation/Provinces.xml"));

            DataRow[] drs = ds.Tables["state"].Select("stateid= '" + ID + "'");
            DataRow[] drs = ds.Tables["province "].Select("province id= '" + ID + "'");

            if (drs.Length != 0)
            {
                return drs[0]["name"].ToString();
            }
            else
            {
                return String.Empty;
            }
        }
States.xml
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<states>
  <state stateid="AL" name="Alabama" />
 
  <state stateid="AK" name="Alaska" />
 
  <state stateid="AZ" name="Arizona" />
 
  <state stateid="AR" name="Arkansas" />
 
  <state stateid="CA" name="California" />
 
  <state stateid="CO" name="Colorado" />
 
  <state stateid="CT" name="Connecticut" />
 
  <state stateid="DC" name="District of Columbia" />
 
  <state stateid="DE" name="Delaware" />
 
  <state stateid="FL" name="Florida" />
 
  <state stateid="GA" name="Georgia" />
 
  <state stateid="HI" name="Hawaii" />
 
  <state stateid="ID" name="Idaho" />
 
  <state stateid="IL" name="Illinois" />
 
  <state stateid="IN" name="Indiana" />
 
  <state stateid="IA" name="Iowa" />
 
  <state stateid="KS" name="Kansas" />
 
  <state stateid="KY" name="Kentucky" />
 
  <state stateid="LA" name="Louisiana" />
 
  <state stateid="ME" name="Maine" />
 
  <state stateid="MD" name="Maryland" />
 
  <state stateid="MA" name="Massachusetts" />
 
  <state stateid="MI" name="Michigan" />
 
  <state stateid="MN" name="Minnesota" />
 
  <state stateid="MS" name="Mississippi" />
 
  <state stateid="MO" name="Missouri" />
 
  <state stateid="MT" name="Montana" />
 
  <state stateid="NE" name="Nebraska" />
 
  <state stateid="NV" name="Nevada" />
 
  <state stateid="NH" name="New Hampshire" />
 
  <state stateid="NJ" name="New Jersey" />
 
  <state stateid="NM" name="New Mexico" />
 
  <state stateid="NY" name="New York" />
 
  <state stateid="NC" name="North Carolina" />
 
  <state stateid="ND" name="North Dakota" />
 
  <state stateid="OH" name="Ohio" />
 
  <state stateid="OK" name="Oklahoma" />
 
  <state stateid="OR" name="Oregon" />
 
  <state stateid="PA" name="Pennsylvania" />
 
  <state stateid="RI" name="Rhode Island" />
 
  <state stateid="SC" name="South Carolina" />
 
  <state stateid="SD" name="South Dakota" />
 
  <state stateid="TN" name="Tennessee" />
 
  <state stateid="TX" name="Texas" />
 
  <state stateid="UT" name="Utah" />
 
  <state stateid="VT" name="Vermont" />
 
  <state stateid="VA" name="Virginia" />
 
  <state stateid="WA" name="Washington" />
 
  <state stateid="WV" name="West Virginia" />
 
  <state stateid="WI" name="Wisconsin" />
 
  <state stateid="WY" name="Wyoming" />
 
</states>
 =========================================================
Provinces.xml
 
<?xml version="1.0" encoding="utf-8" ?>
<provinces>
  <!--Canadian Provinces-->
  <province name="Alberta" provinceid="AB" />
 
  <province name="British Columbia" provinceid="BC" />
 
  <province name="Manitoba" provinceid="MB" />
 
  <province name="New Brunswick" provinceid="NB" />
 
  <province name="Newfoundland and Labrador" provinceid="NL" />
 
  <province name="Nova Scotia" provinceid="NS" />
 
  <province name="Northwest Territories" provinceid="NT" />
 
  <province name="Nunavut" provinceid="NU" />
 
  <province name="Ontario" provinceid="ON" />
 
  <province name="Prince Edward Island" provinceid="PE" />
 
  <province name="Quebec" provinceid="QC" />
 
  <province name="Saskatchewan" provinceid="SK" />
 
  <province name="Yukon" provinceid="YT" />
 
</provinces>

Open in new window

mathieu_cuprykAsked:
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.

Expert1701Commented:
If I had to use the XML files, I would probably do the following:

    string GetProvinceOrStateName(string ID)
    {
        return GetProvinceName(ID) ?? GetStateName(ID);
    }

    string GetProvinceName(string ID)
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("GeneralInformation/Provinces.xml"));
        DataRow[] drs = ds.Tables["province"].Select("provinceid= '" + ID + "'");
        return drs.Length == 0 ? null : drs[0]["name"].ToString();
    }

    string GetStateName(string ID)
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("GeneralInformation/States.xml"));
        DataRow[] drs = ds.Tables["state"].Select("stateid= '" + ID + "'");
        return drs.Length == 0 ? null : drs[0]["name"].ToString();
    }

And I would use it like this:

  lblLocation.Text =  reader.GetString(22).ToString() + ", "  (GetProvincesorStatesName(reader.GetString(20)) ?? "") + GetCountryName(reader.GetString(19));
0

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
mathieu_cuprykAuthor Commented:
return GetProvinceName(ID) ?? GetStateName(ID);

what is the ??
mean?
0
Fernando SotoRetiredCommented:
Hi mathieu_cupryk;

Here you go. It should work fine.

Fernando
public string GetProvincesorStatesName(string ID)
{
    DataSet ds = new DataSet();
    ds.ReadXml(Server.MapPath("GeneralInformation/States.xml"));
    DataRow[] drsState = ds.Tables["state"].Select("stateid= '" + ID + "'");
 
    if (drsState.Length != 0)
    {
        return drsState[0]["name"].ToString();
    }
    else
    {
        ds.Reset();
        ds.ReadXml(Server.MapPath("GeneralInformation/Provinces.xml"));
        DataRow[] drsProvince = ds.Tables["province"].Select("provinceid= '" + ID + "'");
        if (drsProvince.Length != 0)
        {
            return drsProvince[0]["name"].ToString();
        }
    }
    return String.Empty;
}

Open in new window

0
Expert1701Commented:
The ?? operator returns the first value if that value is not null, otherwise it returns the second value (see: http://msdn2.microsoft.com/en-us/library/ms173224(VS.80).aspx).  So the statement,

  return GetProvinceName(ID) ?? GetStateName(ID);

is the same as the following.

  string provinceName = GetProvinceName(ID);

  if(provinceName == null)
  {
    return GetStateName(ID);
  }
  else
  {
    return provinceName;
  }
0
Expert1701Commented:
And a correction, this should have read:

lblLocation.Text =  reader.GetString(22).ToString() + ", " + (GetProvinceOrStateName(reader.GetString(20)) ?? "") + GetCountryName(reader.GetString(19));
0
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.