【问题标题】:Reading Specific Values From XML in C#在 C# 中从 XML 读取特定值
【发布时间】:2016-12-22 16:12:23
【问题描述】:

我使用天气 API 以 XML 格式返回温度值并将它们写入文本文件。我的下一步是从 XML 文件中读取值以在我的程序中使用。这是值的格式;

<temperature value="21.37" min="18.89" max="22.78" unit="metric">
</temperature>
<humidity value="68" unit="%">
</humidity>
<pressure value="1019" unit="hPa">
</pressure>

我想访问温度值,但我不确定如何通过从文本文件中读取来做到这一点,特别是考虑到文本文件比我需要的要长得多。访问我想要的值的最有效方法是什么?

编辑:

<current>
  <city id="" name="">
    <coord lon="-0.45" lat="52.19">
    </coord>
    <country>GB</country>
    <sun rise="2016-08-16T04:48:13" set="2016-08-16T19:22:26">
    </sun>
  </city>
  <temperature value="22.06" min="19.44" max="23.89" unit="metric">
  </temperature>
  <humidity value="67" unit="%">
  </humidity>
  <pressure value="1019" unit="hPa">
  </pressure>
  <wind>
    <speed value="2.57" name="Light breeze">
    </speed>
    <gusts value="6.17">
    </gusts>
    <direction value="73" code="ENE" name="East-northeast">
    </direction>
  </wind>
  <clouds value="24" name="few clouds">
  </clouds>
  <visibility>
  </visibility>
  <precipitation mode="no">
  </precipitation>
  <weather number="801" value="few clouds" icon="02d">
  </weather>
  <lastupdate value="2016-08-16T10:44:02">
  </lastupdate>
</current>

【问题讨论】:

    标签: c# xml file weather-api


    【解决方案1】:

    一种方法可以是:

    var result = XDocument.Load("data.xml").Root
                          .Element(/*.... the rest of the hierarchy.. */)
                          .Element("temperature")
                          .Attribute("value").Value;
    

    如果您不想指定元素的完整方式,您可以:

    var result = XDocument.Load("data.xml").Root
                          .Descendants("temperature")
                          .Select(element => element.Attribute("value").Value).FirstOrDefault();
    

    【讨论】:

    • 我在使用这种方法时遇到了错误,Additional information: Data at the root level is invalid. Line 1, position 1。我已经用完整的 XML 文件更新了我的问题。
    • @CBreeze - 我现在已经在您给定的 xml 上执行了两个代码部分,这很好。你确定你的输出目录下的xml是一样的吗?
    【解决方案2】:

    您可以使用SelectSingleNode,前提是您使用XDocument 加载了XML。

    看看https://msdn.microsoft.com/en-ca/library/fb63z0tw.aspx

    【讨论】:

      【解决方案3】:

      使用xml linq

      using System;
      using System.Globalization;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Xml;
      using System.Xml.Linq;
      
      namespace ConsoleApplication7
      {
          class Program
          {
              const string FILENAME = @"c:\temp\test.xml";
              static void Main(string[] args)
              {
                  XDocument doc = XDocument.Load(FILENAME);
                  var results = doc.Descendants("current").Select(x => new {
                     temperature = x.Elements("temperature").Select(y => new {
                         value = (decimal)y.Attribute("value"),
                         min = (decimal)y.Attribute("min"),
                         max = (decimal)y.Attribute("max"),
                         unit = (string)y.Attribute("unit")
                     }).FirstOrDefault(),
                     humidity = x.Elements("humidity").Select(y => new
                     {
                         value = (decimal)y.Attribute("value"),
                         unit = (string)y.Attribute("unit")
                     }).FirstOrDefault(),
                     pressure = x.Elements("pressure").Select(y => new
                     {
                         value = (decimal)y.Attribute("value"),
                         unit = (string)y.Attribute("unit")
                     }).FirstOrDefault()
      
      
                  }).FirstOrDefault();
              }
           }
      }
      

      【讨论】:

        【解决方案4】:
        var xml = XElement.Parse("<root><temperature value=\"21.37\" min=\"18.89\" max=\"22.78\" unit=\"metric\"></temperature><humidity value=\"68\" unit=\"%\"></humidity><pressure value=\"1019\" unit=\"hPa\"></pressure></root>"); // this line is just to test. You can use what you are getting from API Call.
        
        var result =   xml.Elements("temperature")
                                    .Select(c => c.Attribute("value").Value); // the attribute value
        

        【讨论】:

          猜你喜欢
          • 2016-02-28
          • 1970-01-01
          • 2017-06-08
          • 1970-01-01
          • 2016-03-23
          • 2020-02-25
          • 2018-12-05
          • 1970-01-01
          相关资源
          最近更新 更多