2016-08-16 66 views
1

我已經使用天氣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> 

回答

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

我得到這個方法的錯誤'附加信息:在根級別的數據是無效的。第1行,位置1'。我已經用完整的XML文件更新了我的問題。 – CBreeze

+0

@CBreeze - 我現在已經在給定的xml上執行了兩個代碼段,這很好。你確定輸出目錄中的xml是一樣的嗎? –

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

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