Click here to Skip to main content
15,936,565 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Could someone please explain to me why this is happening and how to fix it? I have some code to check if nodes in an online XML file are empty or not. If they are not empty, it should put the values into labels and if they are empty it should set the text of the label to nothing. I have some code to do this however it does not work well. The code I am using is as follows:

C#
SetF_GroupHourlyObservations.Text = string.Concat("Hourly Observations for '", SetF_SelectSite.SelectedItem, "'");
            DataDoc.Load("http://datapoint.metoffice.gov.uk/public/data/val/wxobs/all/xml/3808?res=hourly&key=-");
if (DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[23] != null)
            { SetF_WindGust01.Text = DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[23].InnerText + "mph"; }
            else { SetF_WindGust01.Text = ""; }
            if (DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[22] != null)
            { SetF_WindGust02.Text = DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[22].InnerText + "mph"; }
            else { SetF_WindGust02.Text = ""; }
            if (DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[21] != null)
            { SetF_WindGust03.Text = DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[21].InnerText + "mph"; }
            else { SetF_WindGust03.Text = ""; }
            if (DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[20] != null)
            { SetF_WindGust04.Text = DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[20].InnerText + "mph"; }
            else { SetF_WindGust04.Text = ""; }
            if (DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[19] != null)
            { SetF_WindGust05.Text = DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[19].InnerText + "mph"; }
            else { SetF_WindGust05.Text = ""; }
            if (DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[18] != null)
            { SetF_WindGust06.Text = DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[18].InnerText + "mph"; }
            else { SetF_WindGust06.Text = ""; }
            if (DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[17] != null)
            { SetF_WindGust07.Text = DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[17].InnerText + "mph"; }
            else { SetF_WindGust07.Text = ""; }
            if (DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[16] != null)
            { SetF_WindGust08.Text = DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[16].InnerText + "mph"; }
            else { SetF_WindGust08.Text = ""; }
            if (DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[15] != null)
            { SetF_WindGust09.Text = DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[15].InnerText + "mph"; }
            else { SetF_WindGust09.Text = ""; }
            if (DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[14] != null)
            { SetF_WindGust10.Text = DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[14].InnerText + "mph"; }
            else { SetF_WindGust10.Text = ""; }
            if (DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[13] != null)
            { SetF_WindGust11.Text = DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[13].InnerText + "mph"; }
            else { SetF_WindGust11.Text = ""; }
            if (DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[12] != null)
            { SetF_WindGust12.Text = DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[12].InnerText + "mph"; }
            else { SetF_WindGust12.Text = ""; }
            if (DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[11] != null)
            { SetF_WindGust13.Text = DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[11].InnerText + "mph"; }
            else { SetF_WindGust13.Text = ""; }
            if (DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[10] != null)
            { SetF_WindGust14.Text = DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[10].InnerText + "mph"; }
            else { SetF_WindGust14.Text = ""; }
            if (DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[9] != null)
            { SetF_WindGust15.Text = DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[9].InnerText + "mph"; }
            else { SetF_WindGust15.Text = ""; }
            if (DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[8] != null)
            { SetF_WindGust16.Text = DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[8].InnerText + "mph"; }
            else { SetF_WindGust16.Text = ""; }
            if (DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[7] != null)
            { SetF_WindGust17.Text = DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[7].InnerText + "mph"; }
            else { SetF_WindGust17.Text = ""; }
            if (DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[6] != null)
            { SetF_WindGust18.Text = DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[6].InnerText + "mph"; }
            else { SetF_WindGust18.Text = ""; }
            if (DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[5] != null)
            { SetF_WindGust19.Text = DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[5].InnerText + "mph"; }
            else { SetF_WindGust19.Text = ""; }
            if (DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[4] != null)
            { SetF_WindGust20.Text = DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[4].InnerText + "mph"; }
            else { SetF_WindGust20.Text = ""; }
            if (DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[3] != null)
            { SetF_WindGust21.Text = DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[3].InnerText + "mph"; }
            else { SetF_WindGust21.Text = ""; }
            if (DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[2] != null)
            { SetF_WindGust22.Text = DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[2].InnerText + "mph"; }
            else { SetF_WindGust22.Text = ""; }
            if (DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[1] != null)
            { SetF_WindGust23.Text = DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[1].InnerText + "mph"; }
            else { SetF_WindGust23.Text = ""; }
            if (DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[0] != null)
            { SetF_WindGust24.Text = DataDoc.SelectNodes("/SiteRep/DV/Location/Period/Rep/@G")[0].InnerText + "mph"; }
            else { SetF_WindGust24.Text = ""; }


The XML I am reading is as follows:

XML
<SiteRep>
<Wx>
<Param name="G" units="mph">Wind Gust</Param>
<Param name="T" units="C">Temperature</Param>
<Param name="V" units="m">Visibility</Param>
<Param name="D" units="compass">Wind Direction</Param>
<Param name="S" units="mph">Wind Speed</Param>
<Param name="W" units="">Weather Type</Param>
<Param name="P" units="hpa">Pressure</Param>
</Wx>
<DV dataDate="2013-09-04T15:00:00Z" type="Obs">
<Location i="3808" lat="50.218" lon="-5.33" name="CAMBORNE" country="ENGLAND" continent="EUROPE">
<Period type="Day" value="2013-09-03Z">
<Rep D="NNW" P="1026" S="6" T="18.8" V="25000" W="1">960</Rep>
<Rep D="NNW" P="1025" S="2" T="18.8" V="25000" W="1">1020</Rep>
<Rep D="N" P="1025" S="3" T="18.4" V="30000" W="1">1080</Rep>
<Rep D="WNW" P="1025" S="3" T="16.5" V="35000" W="0">1140</Rep>
<Rep D="NW" P="1025" S="1" T="14.3" V="12000" W="0">1200</Rep>
<Rep P="1025" S="0" T="13.5" V="6000" W="0">1260</Rep>
<Rep D="E" P="1025" S="2" T="12.6" V="800" W="6">1320</Rep>
<Rep D="ESE" P="1024" S="5" T="12.1" V="10000" W="2">1380</Rep>
</Period>
<Period type="Day" value="2013-09-04Z">
<Rep D="S" P="1024" S="3" T="12.2" V="12000" W="0">0</Rep>
<Rep D="SE" P="1023" S="5" T="11.8" V="8000" W="5">60</Rep>
<Rep D="S" P="1023" S="5" T="10.8" V="400" W="6">120</Rep>
<Rep D="SSE" P="1023" S="3" T="12.6" V="400" W="6">180</Rep>
<Rep D="SSE" P="1022" S="1" T="13.2" V="5000" W="7">240</Rep>
<Rep D="SE" P="1022" S="5" T="13.9" V="12000" W="7">300</Rep>
<Rep D="SSW" P="1022" S="2" T="12.6" V="4000" W="5">360</Rep>
<Rep D="SSE" P="1021" S="5" T="13.1" V="6000" W="5">420</Rep>
<Rep D="SE" P="1021" S="3" T="14.7" V="10000" W="7">480</Rep>
<Rep D="SE" P="1021" S="8" T="15.6" V="12000" W="7">540</Rep>
<Rep D="SE" P="1020" S="9" T="17.7" V="10000" W="7">600</Rep>
<Rep D="SE" P="1020" S="15" T="17.8" V="10000" W="7">660</Rep>
<Rep D="SE" P="1019" S="13" T="19.9" V="10000" W="7">720</Rep>
<Rep D="SSE" P="1018" S="10" T="19.0" V="7000" W="7">780</Rep>
<Rep D="SSE" P="1018" S="13" T="16.6" V="5000" W="7">840</Rep>
<Rep D="SSE" P="1018" S="10" T="17.1" V="6000" W="7">900</Rep>
</Period>
</Location>
</DV>
</SiteRep
>


All of the labels are empty but this code should work shouldn't it? Also in other sets of data, if one record is missing then they the missing record doesn't show as empty like I set it to in the code? Why? What should happen is all the labels should be filled with the relevant data and if the node is empty or doesn't exist then it should set the relevant label to empty. Thank you.
Posted
Comments
Sergey Alexandrovich Kryukov 4-Sep-13 13:00pm    
The whole approach looks so wrong. You hard-code everything, repeat the same statement or expression many times. It has nothing to do with programming. If you need help, try to explain what do you want to achieve, ultimately. But you should not do what you are doing.
—SA
Henry Hunt 4-Sep-13 14:08pm    
But surely if I have 24 individual labels and I want each one to contain a different value from a different attribute from the same document and I don't want to use any type of list controls then that is how to do it? I can't think of any other way to populate different labels with different values other than one by one.
Sergey Alexandrovich Kryukov 4-Sep-13 14:56pm    
It does not justify such a sloppy code. It will lead you nowhere. Labels? You should not have individual labels, you should create an array (or other collection) of then during runtime. And so on...
—SA

Aside from what SA said about your programming style your problem is that none of the Rep nodes have an attribute named G, but that is what you're trying to select with your XPath.

Cheers!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900