从C#中读取XML中的特定值

问题描述:

我已经使用天气API以XML格式返回温度值并将它们写入文本文件。我的下一步是从我的程序中读取XML文件中的值。这是值的格式;从C#中读取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> 

的一种方式可以是:

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(); 
+0

我得到这个方法的错误'附加信息:在根级别的数据是无效的。第1行,位置1'。我已经用完整的XML文件更新了我的问题。 – CBreeze

+0

@CBreeze - 我现在已经在给定的xml上执行了两个代码段,这很好。你确定输出目录中的xml是一样的吗? –

您可以使用SelectSingleNode提供您所加载的XML与XDocument

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

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 

Usse 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(); 
     } 
    } 
}