Reputation: 1190
My Problem: I am developing an IOS App at the Moment, with Xamarin.IOS. I mostly worked with JSON Webservices so far, but now i have to use a weather - API that is returning an XML string. I can't figure out how to parse it, so i can access its data in my code.
Solving attempts: I already went through heaps of c# tutorials, for parsing it to an array, a dictionary, a custom object etc etc, but i have the problem that most of them require librarys that i can't access within mono touch.
But there has to be an equivalent.
I tried to convert it to a JSON, so i can use it as usual, thing here is, for example the method
JsonConvert.SerializeXmlNode(...);
Is not available in Monotouch. The assembly reference is there, just some specific methods are missing.
Can anybody suggest a solution or just point me into a direction? Any light you can shed on this is really appreciated. Thanks.
Code P.S: This is what i get when requesting the web service.
<?xml version="1.0" encoding="UTF-8" ?>
<weather>
<data>
<location>
<pcode>21335</pcode>
</location>
<day date="07.11.2014">
<tmax>10</tmax>
<tmin>2</tmin>
<wrcode>13</wrcode>
<symbol>regenschauer</symbol>
<rr>20</rr>
<windforce_bft>4</windforce_bft>
<windforce_kmh>22</windforce_kmh>
<winddir>SO</winddir>
<sunrise>07:28</sunrise>
<sunset>16:36</sunset>
<sunshine>6</sunshine>
<details>
<temp_m>6</temp_m>
<symbol_m>heiter</symbol_m>
<wrcode_m>4</wrcode_m>
<windforce_bft_m>4</windforce_bft_m>
<windforce_kmh_m>20</windforce_kmh_m>
<winddir_m>S</winddir_m>
<rr_m>5</rr_m>
<temp_a>10</temp_a>
<symbol_a>wolkig</symbol_a>
<wrcode_a>5</wrcode_a>
<windforce_bft_a>4</windforce_bft_a>
<windforce_kmh_a>22</windforce_kmh_a>
<winddir_a>SO</winddir_a>
<rr_a>20</rr_a>
<temp_e>9</temp_e>
<wrcode_e>13</wrcode_e>
<symbol_e>regenschauer</symbol_e>
<windforce_bft_e>4</windforce_bft_e>
<windforce_kmh_e>22</windforce_kmh_e>
<winddir_e>S</winddir_e>
<rr_e>70</rr_e>
<temp_n>6</temp_n>
<wrcode_n>5</wrcode_n>
<symbol_n>wolkig_n</symbol_n>
<windforce_bft_n>4</windforce_bft_n>
<windforce_kmh_n>20</windforce_kmh_n>
<winddir_n>S</winddir_n>
<rr_n>40</rr_n>
</details>
</day>
<day date="08.11.2014">
<tmax>11</tmax>
<tmin>6</tmin>
<wrcode>5</wrcode>
<symbol>wolkig</symbol>
<rr>30</rr>
<windforce_bft>4</windforce_bft>
<windforce_kmh>22</windforce_kmh>
<winddir>SW</winddir>
<sunrise>07:30</sunrise>
<sunset>16:34</sunset>
<sunshine>3</sunshine>
<details>
<temp_m>9</temp_m>
<symbol_m>bedeckt</symbol_m>
<wrcode_m>6</wrcode_m>
<windforce_bft_m>4</windforce_bft_m>
<windforce_kmh_m>28</windforce_kmh_m>
<winddir_m>S</winddir_m>
<rr_m>30</rr_m>
<temp_a>11</temp_a>
<symbol_a>wolkig</symbol_a>
<wrcode_a>5</wrcode_a>
<windforce_bft_a>4</windforce_bft_a>
<windforce_kmh_a>22</windforce_kmh_a>
<winddir_a>SW</winddir_a>
<rr_a>15</rr_a>
<temp_e>10</temp_e>
<wrcode_e>5</wrcode_e>
<symbol_e>wolkig</symbol_e>
<windforce_bft_e>3</windforce_bft_e>
<windforce_kmh_e>19</windforce_kmh_e>
<winddir_e>S</winddir_e>
<rr_e>10</rr_e>
<temp_n>6</temp_n>
<wrcode_n>5</wrcode_n>
<symbol_n>wolkig_n</symbol_n>
<windforce_bft_n>3</windforce_bft_n>
<windforce_kmh_n>19</windforce_kmh_n>
<winddir_n>SO</winddir_n>
<rr_n>10</rr_n>
</details>
</day>
<day date="09.11.2014">
<tmax>11</tmax>
<tmin>6</tmin>
<wrcode>5</wrcode>
<symbol>wolkig</symbol>
<rr>30</rr>
<windforce_bft>4</windforce_bft>
<windforce_kmh>24</windforce_kmh>
<winddir>SO</winddir>
<sunrise>07:32</sunrise>
<sunset>16:32</sunset>
<sunshine>3</sunshine>
<details>
<temp_m>9</temp_m>
<symbol_m>wolkig</symbol_m>
<wrcode_m>5</wrcode_m>
<windforce_bft_m>4</windforce_bft_m>
<windforce_kmh_m>22</windforce_kmh_m>
<winddir_m>SO</winddir_m>
<rr_m>15</rr_m>
<temp_a>11</temp_a>
<symbol_a>bedeckt</symbol_a>
<wrcode_a>6</wrcode_a>
<windforce_bft_a>4</windforce_bft_a>
<windforce_kmh_a>24</windforce_kmh_a>
<winddir_a>SO</winddir_a>
<rr_a>30</rr_a>
<temp_e>10</temp_e>
<wrcode_e>5</wrcode_e>
<symbol_e>wolkig</symbol_e>
<windforce_bft_e>3</windforce_bft_e>
<windforce_kmh_e>19</windforce_kmh_e>
<winddir_e>S</winddir_e>
<rr_e>10</rr_e>
<temp_n>7</temp_n>
<wrcode_n>5</wrcode_n>
<symbol_n>wolkig_n</symbol_n>
<windforce_bft_n>3</windforce_bft_n>
<windforce_kmh_n>17</windforce_kmh_n>
<winddir_n>SW</winddir_n>
<rr_n>20</rr_n>
</details>
</day>
<day date="10.11.2014">
<tmax>10</tmax>
<tmin>7</tmin>
<wrcode>13</wrcode>
<symbol>regenschauer</symbol>
<rr>60</rr>
<windforce_bft>3</windforce_bft>
<windforce_kmh>19</windforce_kmh>
<winddir>S</winddir>
<sunrise>07:34</sunrise>
<sunset>16:31</sunset>
<sunshine>2</sunshine>
</day>
<day date="11.11.2014">
<tmax>11</tmax>
<tmin>7</tmin>
<wrcode>5</wrcode>
<symbol>wolkig</symbol>
<rr>20</rr>
<windforce_bft>4</windforce_bft>
<windforce_kmh>20</windforce_kmh>
<winddir>SO</winddir>
<sunrise>07:35</sunrise>
<sunset>16:29</sunset>
<sunshine>5</sunshine>
</day>
<day date="12.11.2014">
<tmax>11</tmax>
<tmin>8</tmin>
<wrcode>5</wrcode>
<symbol>wolkig</symbol>
<rr>20</rr>
<windforce_bft>4</windforce_bft>
<windforce_kmh>20</windforce_kmh>
<winddir>O</winddir>
<sunrise>07:37</sunrise>
<sunset>16:27</sunset>
<sunshine>4</sunshine>
</day>
<actual_weather>
<date_time>Freitag, 23:00 Uhr</date_time>
<symbol>bedeckt_n</symbol>
<temp>7,4</temp>
<weatherdesc>Stark bewölkt</weatherdesc>
<windforce_bft>3</windforce_bft>
<windforce_kmh>14</windforce_kmh>
<winddir>SSW</winddir>
</actual_weather>
</data>
Upvotes: 1
Views: 2022
Reputation: 136
Monotouch includes the System.Xml namespace by default: http://developer.xamarin.com/guides/ios/under_the_hood/assemblies/
You can analyze the data in an xmldocument as follows:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(dataString);
foreach (XmlElement x in xmlDoc.SelectNodes("weather/data/location/pcode"))
{
Console.Write(x.InnerXml);
}
dataString
is a String
containing XML. If you want to use a Stream, use xmlDoc.Load(Stream)
instead.
The argument for SelectNodes
is an XPath expression. I know people don't like W3Schools, but they have a decent Xpath tutorial here: http://www.w3schools.com/xpath/
Upvotes: 2