繁体   English   中英

转换/处理XML的数据-Monotouch(Xamarin)/ C#中的字符串

[英]Converting / Acessing the Data of an XML - String in monotouch (Xamarin) / C#

我的问题:我目前正在使用Xamarin.IOS开发IOS应用程序。 到目前为止,我主要使用JSON Web服务,但是现在我必须使用天气-返回XML字符串的API。 我不知道如何解析它,所以我可以在代码中访问它的数据。

解决尝试:我已经遍历了c#教程堆,将其解析为数组,字典,自定义对象等,但是我遇到的问题是,它们中的大多数都需要我无法在mono touch中访问的库。

但是必须有一个等效的。

我试图将其转换为JSON,所以我可以像往常一样使用它,例如方法

JsonConvert.SerializeXmlNode(...);

在Monotouch中不可用。 那里有汇编参考,只是缺少一些特定的方法。

任何人都可以提出解决方案或只是指出我的方向吗? 您可以对此提出的任何想法,我们深表感谢。 谢谢。

代码 PS:这是请求Web服务时得到的信息。

<?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>

Monotouch默认包含System.Xml命名空间: http : //developer.xamarin.com/guides/ios/under_the_hood/assemblies/

您可以按以下方式分析xml文档中的数据:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(dataString);
foreach (XmlElement x in xmlDoc.SelectNodes("weather/data/location/pcode"))
{
     Console.Write(x.InnerXml);
}

dataString是包含XML的String 如果要使用Stream,请改用xmlDoc.Load(Stream) SelectNodes的参数是XPath表达式。 我知道人们不喜欢W3Schools,但他们在这里有不错的Xpath教程: http : //www.w3schools.com/xpath/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM