• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1096
  • Last Modified:

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

0
mathieu_cupryk
Asked:
mathieu_cupryk
  • 3
2 Solutions
 
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
 
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now