C# 在C中从XML中读取特定值
我使用天气API以XML格式返回温度值,并将其写入文本文件。我的下一步是从XML文件中读取中的值,以便在我的程序中使用。这是值的格式C# 在C中从XML中读取特定值,c#,xml,file,weather-api,C#,Xml,File,Weather Api,我使用天气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
<temperature value="21.37" min="18.89" max="22.78" unit="metric">
</temperature>
<humidity value="68" unit="%">
</humidity>
<pressure value="1019" unit="hPa">
</pressure>
我想访问温度值,但我不确定如何从文本文件中读取,尤其是考虑到文本文件比我需要的长很多。访问我想要的值的最有效方法是什么
编辑:
如果使用XDocument加载XML,则可以使用SelectSingleNode
看看一种方法可以是:
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();
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();
}
}
}
这个方法有一个错误,附加信息:根级别的数据无效。第1行,位置1。我已经用完整的XML文件更新了我的问题。@CBreeze-我现在已经对给定的XML执行了两个代码部分,一切正常。您确定输出目录中的xml相同吗?
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
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();
}
}
}