• <bdo id='FshGd'></bdo><ul id='FshGd'></ul>
  • <i id='FshGd'><tr id='FshGd'><dt id='FshGd'><q id='FshGd'><span id='FshGd'><b id='FshGd'><form id='FshGd'><ins id='FshGd'></ins><ul id='FshGd'></ul><sub id='FshGd'></sub></form><legend id='FshGd'></legend><bdo id='FshGd'><pre id='FshGd'><center id='FshGd'></center></pre></bdo></b><th id='FshGd'></th></span></q></dt></tr></i><div id='FshGd'><tfoot id='FshGd'></tfoot><dl id='FshGd'><fieldset id='FshGd'></fieldset></dl></div>

  • <tfoot id='FshGd'></tfoot>
  • <small id='FshGd'></small><noframes id='FshGd'>

    1. <legend id='FshGd'><style id='FshGd'><dir id='FshGd'><q id='FshGd'></q></dir></style></legend>

        C# 从 XML 中提取数据

        时间:2023-08-28
        <i id='eg5pA'><tr id='eg5pA'><dt id='eg5pA'><q id='eg5pA'><span id='eg5pA'><b id='eg5pA'><form id='eg5pA'><ins id='eg5pA'></ins><ul id='eg5pA'></ul><sub id='eg5pA'></sub></form><legend id='eg5pA'></legend><bdo id='eg5pA'><pre id='eg5pA'><center id='eg5pA'></center></pre></bdo></b><th id='eg5pA'></th></span></q></dt></tr></i><div id='eg5pA'><tfoot id='eg5pA'></tfoot><dl id='eg5pA'><fieldset id='eg5pA'></fieldset></dl></div>
          <bdo id='eg5pA'></bdo><ul id='eg5pA'></ul>
        • <small id='eg5pA'></small><noframes id='eg5pA'>

                    <tbody id='eg5pA'></tbody>
                • <legend id='eg5pA'><style id='eg5pA'><dir id='eg5pA'><q id='eg5pA'></q></dir></style></legend>
                  <tfoot id='eg5pA'></tfoot>

                • 本文介绍了C# 从 XML 中提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试从 URL 中的 XML 读取天气数据.XML 如下所示:

                  I'm trying to read weather data from XML in a URL. The XML looks like this:

                  <weatherdata>
                  <location>...</location>
                  <credit>...</credit>
                  <links>...</links>
                  <meta>...</meta>
                  <sun rise="2013-05-11T04:49:22" set="2013-05-11T21:39:03"/>
                  <forecast>
                  <text>...</text>
                  <tabular>
                  <time from="2013-05-11T01:00:00" to="2013-05-11T06:00:00" period="0">
                  <!--
                   Valid from 2013-05-11T01:00:00 to 2013-05-11T06:00:00 
                  -->
                  <symbol number="2" name="Fair" var="mf/02n.03"/>
                  <precipitation value="0" minvalue="0" maxvalue="0.1"/>
                  <!--  Valid at 2013-05-11T01:00:00  -->
                  <windDirection deg="173.8" code="S" name="South"/>
                  <windSpeed mps="4.2" name="Gentle breeze"/>
                  <temperature unit="celsius" value="9"/>
                  <pressure unit="hPa" value="1004.2"/>
                  </time>
                  </tabular>
                  </forecast>
                  <observations>...</observations>
                  </weatherdata>
                  

                  我对 XML 中的预测数据感兴趣.我想获取时间和时间,然后是天气数据.例如温度在 XML 中是这样写的:

                  I am interested in the forecast data in the XML. I want to get the time from and time to, then the weather data. For example the temperature is written like this in the XML:

                  <temperature unit="celsius" value="9"/>
                  

                  我想用这样的方式提取数据:

                  I want to extract the data with something like this:

                   string fromTime = time from(the attribute in the xml);
                                       string fromTime =time to(the attribute in the xml);
                                      string name = temperature(the attribute in the xml);
                                      string unit =unit(the attribute in the xml);
                                      int value = value(the attribute in the xml);
                  

                  我创建了能够读取所有内容的示例代码,但我不知道如何仅提取我需要的数据.我现在的代码如下所示:

                  I created sample code that is able to read everything but I don't know how to extract just the data I need. The code I have now looks like this:

                          String URLString = "http://www.yr.no/place/Norway/Oslo/Oslo/Oslo/forecast.xml";
                          XmlTextReader reader = new XmlTextReader(URLString);
                  
                          while (reader.Read())
                          {
                              switch (reader.NodeType)
                              {
                                  case XmlNodeType.Element: // The node is an element.
                                      Console.Write("" + reader.Name);
                  
                                      while (reader.MoveToNextAttribute()) // Read the attributes.
                                          Console.Write(" " + reader.Name + "='" + reader.Value + "'");
                                      Console.Write("
                  ");
                                      Console.WriteLine("------------------------------");
                                      break;
                                  case XmlNodeType.Text: //Display the text in each element.
                                      Console.WriteLine(reader.Value);
                                      break;
                                  case XmlNodeType.EndElement: //Display the end of the element.
                                      Console.Write("</" + reader.Name);
                                      Console.WriteLine(">");
                                      break;
                  
                              }
                          }
                  

                  有什么方法可以只提取天气数据和时间吗?

                  Any ideas how I can extract just the weather data and the time?

                  推荐答案

                  使用 LINQ to XML

                  Use LINQ to XML

                  XDocument X = XDocument.Load("http://www.yr.no/place/Norway/Oslo/Oslo/Oslo/forecast.xml");
                  
                  var forecast = X.Element("weatherdata").Element("forecast");
                  var location = forecast.Descendants("location").Attributes("name").FirstOrDefault().Value;
                  var tempData = forecast.Element("tabular").Elements("time");
                  
                  //This is what you need
                  var data = tempData.Select(item=>
                              new{
                                  from = Convert.ToDateTime(item.Attribute("from").Value),
                                  to = Convert.ToDateTime(item.Attribute("to").Value),
                                  temp = item.Element("temperature").Attribute("value").Value
                              });
                  
                  
                  //Or you can do a foreach if you need to
                  foreach (var item in tempData)
                  {
                          DateTime from = Convert.ToDateTime(item.Attribute("from").Value);
                          var temp = item.Element("temperature").Attribute("value").Value;
                  }
                  

                  我还没有填写所有内容.我希望您了解如何使用它.

                  I haven't populated everything. I hope you get the idea of how to use it.

                  这篇关于C# 从 XML 中提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:读取 XML(从字符串)并获取一些字段 - 读取 XML 时出 下一篇:将 XML 转换为通用列表

                  相关文章

                  最新文章

                • <small id='ijaWZ'></small><noframes id='ijaWZ'>

                    <tfoot id='ijaWZ'></tfoot>

                      • <bdo id='ijaWZ'></bdo><ul id='ijaWZ'></ul>

                      <legend id='ijaWZ'><style id='ijaWZ'><dir id='ijaWZ'><q id='ijaWZ'></q></dir></style></legend>
                    1. <i id='ijaWZ'><tr id='ijaWZ'><dt id='ijaWZ'><q id='ijaWZ'><span id='ijaWZ'><b id='ijaWZ'><form id='ijaWZ'><ins id='ijaWZ'></ins><ul id='ijaWZ'></ul><sub id='ijaWZ'></sub></form><legend id='ijaWZ'></legend><bdo id='ijaWZ'><pre id='ijaWZ'><center id='ijaWZ'></center></pre></bdo></b><th id='ijaWZ'></th></span></q></dt></tr></i><div id='ijaWZ'><tfoot id='ijaWZ'></tfoot><dl id='ijaWZ'><fieldset id='ijaWZ'></fieldset></dl></div>