使用XmLReader将XML数据解析为C#
本文关键字:数据 XmLReader XML 使用 | 更新日期: 2023-09-27 18:01:11
我正在做我的学校项目,关于在http://api.openweathermap.org/data/2.5/forecast/daily?q=London&mode=xml&单位=公制&cnt=17,我只需要温度。这里有一个例子:
<time day="2014-05-01">
<symbol number="500" name="light rain" var="10d"/>
<precipitation value="0.5" type="rain"/>
<temperature day="7.6" min="6.8" max="7.6" night="6.8" eve="7.6" morn="7.6"/>
<clouds value="overcast clouds" all="88" unit="%"/>
</time>
我有两个问题:+我不知道如何获取白天的温度并将其解析为我的变量。我试过这个,但没有用:
string temperature = " ";
while (myXmlReader.Read())
{
if (myXmlReader.NodeType == XmlNodeType.Element && myXmlReader.Name == "time")
{
if (myXmlReader.HasAttributes)
{
if (myXmlReader.GetAttribute("day") != null)
{
if (myXmlReader.GetAttribute("day") == day)
{
if (myXmlReader.NodeType == XmlNodeType.Element && myXmlReader.Name == "temperature")
{
if(myXmlReader.HasAttributes)
{
if (myXmlReader.GetAttribute("day") != null)
{
temperature = myXmlReader.GetAttribute("day");
}
}
}
}
}
}
}
}
Response.Write("temperature = " + temperature + "<br/>");
print为null时的变量temperature
。
我有一个变量
city
,它包含用户输入的信息(从HTML表单(,我想把它放在链接中。因此,我使用另一个变量link
:string link = "http://api.openweathermap.org/data/2.5/forecast/daily?q="+city+"&mode=xml&units=metric&cnt=17"
然后我使用请求页面
WebRequest myRequest = WebRequest.Create(link);
然后,出现了问题
while (myXmlReader.Read())
它说{"根级别的数据无效。第1行,位置1。"}。当我不使用变量link
并像这样直接添加链接时,这种情况不会发生:
WebRequest myRequest = WebRequest.Create(@"http://api.openweathermap.org/data/2.5/forecast/daily?q=London&mode=xml&units=metric&cnt=17");
非常感谢您的帮助!!!
关于变量city
:
<form>
City: <select name="city">
<option value="Helsinki">Helsinki</option>
<option value="Lahti">Lahti</option>
<option value="Tampere">Tampere</option>
<option value="Oulu">Oulu</option>
<option value="Rovaniemi">Rovaniemi</option>
<option value="Espoo">Espoo</option>
<option value="Vantaa">Vantaa</option>
</select> <br/>
<input type="submit" value="Submit to see the result"/>
</form>
<%string city = Request["city"]%>
您在处理元素时错过了一旦找到time
元素就不能也在temperature
元素上的元素。您必须继续读取xml文档,直到找到该元素为止。我结合了几个if来稍微压缩你的代码,但这确实有效:
var city="London";
var url = String.Format(
"http://api.openweathermap.org/data/2.5/forecast/daily?q={0}&mode=xml&units=metric&cnt=17"
, city);
string temperature = String.Empty;
string day ="2014-05-02";
using(var wc = new WebClient())
{
using(var stream = wc.OpenRead(url))
{
using(var myXmlReader = new XmlTextReader(stream))
{
while (myXmlReader.Read())
{
// <time day="2014-05-01">
if (myXmlReader.NodeType == XmlNodeType.Element
&& myXmlReader.Name == "time"
&& myXmlReader.HasAttributes
&& myXmlReader.GetAttribute("day") == day)
{
// find the inner elements
while (myXmlReader.Read())
{
// skip <symbol> <precipitation> <windDirection>
// and <windSpeed>
if (myXmlReader.NodeType == XmlNodeType.Element
&& myXmlReader.Name == "temperature"
&& myXmlReader.HasAttributes
&& myXmlReader.GetAttribute("day") != null)
{
// <temperature day="7.83" min="6.76" max="7.83"
// night="6.76" eve="7.83" morn="7.83"/>
temperature = myXmlReader.GetAttribute("day");
break; // stop reading!
}
}
}
}
}
}
}
您可以使用XDocument并利用Linq获得所需的节点,而不是使用XmlReader读取元素:
string WheaterFromXDocument()
{
var city="London";
var url = String.Format(
"http://api.openweathermap.org/data/2.5/forecast/daily?q={0}&mode=xml&units=metric&cnt=17"
, city);
string temperature = String.Empty;
string day ="2014-05-02";
using(var wc = new WebClient())
{
using(var stream = wc.OpenRead(url))
{
var dayAttr = (from time in XDocument.Load(stream).Descendants("time")
where (string) time.Attribute("day") == day
from tempElem in time.Elements("temperature")
select tempElem.Attribute("day"))
.SingleOrDefault();
if (dayAttr!=null)
{
temperature = dayAttr.Value;
}
}
}
return temperature;
}