在C#中帮助 [英] Accuwheather in C# help

查看:55
本文介绍了在C#中帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

i尝试这个代码读取xml of accuwheater

i想要选择一个像本地一样的节点

和读取属性如city

但我选择节点当我把属性与getattribute代码给我一个错误

我尝试的代码就是这个

谢谢



我尝试过:



WebRequest wbreg;



WebResponse wbresp;



wbreg = WebRequest.Create(http://asus1.accu-weather.com/widget/asus1/ weather-data.asp?location =比萨& metric = 1& langId = 1);



wbresp = wbreg.GetResponse();



XmlDocument dd = new XmlDocument();



dd.Load(http://asus1.accu-weather.com/ widget / asus1 / weather-data.asp?location = Pisa& metric = 1& langId = 1);



XmlElement theElem =((XmlElement)dd.SelectSingleNode (本地));



CittaLabel.Text = theElem.GetAttribute(city)。ToString( );

hello
i try this code for read xml of accuwheater
i want select a node like local
and read attribute like city
but i select node when i put attribute with getattribute the code give me a error
the code that i try is this
thank you

What I have tried:

WebRequest wbreg;

WebResponse wbresp;

wbreg = WebRequest.Create(http://asus1.accu-weather.com/widget/asus1/weather-data.asp?location=Pisa&metric=1&langId=1);

wbresp = wbreg.GetResponse();

XmlDocument dd = new XmlDocument();

dd.Load("http://asus1.accu-weather.com/widget/asus1/weather-data.asp?location=Pisa&metric=1&langId=1");

XmlElement theElem = ((XmlElement)dd.SelectSingleNode("local"));

CittaLabel.Text = theElem.GetAttribute("city").ToString();

推荐答案

查看从该URL返回的XML,特别是< local> 节点:

Look at the XML returned from that URL, and in particular the <local> node:
<local>
    <city>Pisa</city>
    <adminArea code="52">Tuscany</adminArea>
    <country code="IT">Italy</country>
    <cityId>216194</cityId>
    <primaryCityId/>
    <locationKey>216194</locationKey>
    <lat>43.71691</lat>
    <lon>10.40036</lon>
    <time>18:35</time>
    <timeZone>1</timeZone>
    <obsDaylight>1</obsDaylight>
    <currentGmtOffset>2</currentGmtOffset>
    <timeZoneAbbreviation>CEST</timeZoneAbbreviation>
</local>



该节点没有任何属性,因此 theElem .GetAttribute(city)返回 null 。然后尝试在 null 引用上调用 .ToString(),最后获得 NullReferenceException



城市存储在元素中,而不是属性


That node doesn't have any attributes, so theElem.GetAttribute("city") returns null. You then try to call .ToString() on a null reference, and end up getting a NullReferenceException.

The city is stored in an element, not an attribute.

XmlElement city = theElem.SelectSingleNode("city");
CittaLabel.Text = (city == null) ? "Unknown" : city.Value;





但你可以自己找到这个,而不必等待别人回答你的问题,如果你只是尝试调试你的代码!





编辑: XML文档有一个默认命名空间。不幸的是, XmlDocument 和相关类不容易查询具有默认命名空间的节点。您必须创建 XmlNamespaceManager ,添加带有前缀的命名空间,并使用该前缀并将 XmlNamespaceManager 传递给每个拨打 SelectNodes / SelectSingleNode



你'还试图选择 local 元素,该元素是文档的直接后代。文档的唯一直接后代是根< act_database> 元素。您需要使用 XPath [ ^ ]descendant选择器( // aw:local )代替。 />



But you could have found this out for yourself, without having to wait for someone else to answer your question, if you'd only tried debugging your code!


The XML document has a default namespace. Unfortunately, the XmlDocument and related classes don't make it easy to query nodes with a default namespace. You have to create an XmlNamespaceManager, add the namespace with a prefix, and use that prefix and pass the XmlNamespaceManager to every call to SelectNodes / SelectSingleNode.

You're also trying to select the local element which is a direct descendant of the document. The only direct descendant of the document is the root <act_database> element. You need to use the XPath[^] "descendant" selector (//aw:local) instead.

XmlDocument dd = new XmlDocument();
dd.Load("...");

XmlNamespaceManager ns = new XmlNamespaceManager(dd.NameTable);
ns.AddNamespace("aw", "http://www.accuweather.com");

XmlElement theElem = (XmlElement)dd.SelectSingleNode("//aw:local", ns);
XmlElement city = (theElem == null) ? null : (XmlElement)theElem.SelectSingleNode("aw:city", ns);
CittaLabel.Text = (city == null) ? "Unknown" : city.Value;





LINQ to XML [ ^ ]会让这更容易:



LINQ to XML[^] would make this slightly easier:

XDocument dd = XDocument.Load("...");
XNamespace ns = dd.Root.Name.Namespace;
XElement city = dd.Descendants(ns + "city").FirstOrDefault();
CittaLabel.Text = (city == null) ? "Unknown" : city.Value;

这篇关于在C#中帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆