<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://www.webserviceX.NET"><?xml version="1.0" encoding="utf-16"?> <CurrentWeather> <Location>Beijing, China (ZBAA) 39-56N 116-17E 55M</Location> <Time>Feb 22, 2011 - 02:30 AM EST / 2011.02.22 0730 UTC</Time> <Wind> from the SSE (160 degrees) at 4 MPH (4 KT) (direction variable):0</Wind> <Visibility> less than 1 mile:0</Visibility> <SkyConditions> clear</SkyConditions> <Temperature> 46 F (8 C)</Temperature> <DewPoint> 23 F (-5 C)</DewPoint> <RelativeHumidity> 39%</RelativeHumidity> <Pressure> 30.03 in. Hg (1017 hPa)</Pressure> <Status>Success</Status> </CurrentWeather></string>
16:30:59
我的错误的解析代码如下:
private void displayCurrentWeatherData(string xmlContent)
{
try
{
if (xmlContent != string.Empty)
{
XDocument xmlUsers = XDocument.Parse(xmlContent);//Linq
var weather = from user in xmlUsers.Descendants("CurrentWeather")
select new CurrentWeather
{
Location = (string)
user.Element("Location").Value,
Time = (string)
user.Element("Time").Value,
Wind = (string)
user.Element("Wind").Value,
Visibility = (string)
user.Element("Visibility").Value,
SkyConditions = (string)
user.Element("SkyConditions").Value,
Temperature = (string)
user.Element("Temperature").Value,
DewPoint = (string)
user.Element("DewPoint").Value,
RelativeHumidity = (string)
user.Element("RelativeHumidity").Value,
Pressure = (string)
user.Element("Pressure").Value,
Status = (string)
user.Element("City").Value
};
那位高手可以给我讲讲我错在那里呀?
分享到:
相关推荐
.Net中的System.Xml.Linq命名空间提供了linq to xml的支持。这个命名空间中的XDocument,XElement以及XText,XAttribute提供了读写xml文档的关键方法。1. 使用linq to xml写xml:使用XDocument的构造函数可以构造一...
后来 .Net又引入了LINQ,于是LINQ to XML也就应运而生,所以在.Net中,不仅可以用W3C XML DOM标准,还可以使用LINQ to XML来操作XML文档。下面就来简单介绍一下如何使用LINQ to XML。 (一) 加载 加载XML比较常用的...
使用XmlDocument或XDocument创建xml文件,具体内容如下 需引用:System.Xml; System.Xml.Linq; 1.使用XmlDocument创建xml(入门案例) static void Main(string[] args) { //使用XmlDocument创建xml XmlDocument...
linq to xml的一些例子 /**/////根据元素的名称进行筛选(有命名空间) 1 /**/////链接轴方法 2 /**/////链接轴方法,有时,当可能存在或不存在间隔上级时,您希望在特定的元素深度,检索所有的元素 2 /**/////检索...
FluidLinq是一个小的Linq扩展库,它使解析XML非常容易。 该库用于从XML反序列化对象。 它减少了需要编写的代码,并确保您不会遇到无法预见的错误。 创建该库是为了使其易于使用Linq to XML查询。 提供的扩展方法...
一、LINQ to XML 编程基础 1、LINQ to XML类 System.Xml.Linq命名空间含有19个类,下表列出了它们的名称及其描述: 类 描述 XAttribute 表示一个 XML 属性 XCData 表示一个 CDATA 文本节点 XComment ...
Dim doc = XDocument.Load(dataPath & "bib.xml") Dim content = From book In doc...<book> _ From author In book.<author> _ Group book By Key = author.<first>.Value & " " & author.<last>.Value _ Into...
用于LINQ to XML的概念证明Saxon .NET API XDM实现(将Saxon .NET与包装的XDocument结合使用) 注意:这是概念的有效证明。 但是,有几件事情没有实现,也没有经过测试。 概括 saxon-xdoc允许您直接在Saxon的.NET...
我在以前的博客中介绍了如何使用XmlDocument类对XML进行操作,以及如何使用LINQ to XML对XML进行操作。它们分别使用了XmlDocument类和XDocument类。在本文中,我再介绍一个类,XmlTextWriter。我们分别用这三个类将...
在页面的 Page_Load() 方法中写上这句话就行了: 代码如下:new XDocument( new XElement(“ol”, from item in XDocument.Load(“http://cnblogs.com/rss”).Descendants(“item”) select new XElement[]{ new ...
14.7.4 使用LINQ to XML转换XML 14.8 XML数据绑定 14.8.1 非层次化绑定 14.8.2 使用XPath 14.8.3 嵌套的网格 14.8.4 使用TreeView的层次化绑定 14.8.5 使用XSLT 14.8.6 绑定到来自其他源的XML内容 ...
14.7.4 使用LINQ to XML转换XML 502 14.8 XML数据绑定 504 14.8.1 非层次化绑定 504 14.8.2 使用XPath 506 14.8.3 嵌套的网格 508 14.8.4 使用TreeView的层次化绑定 509 14.8.5 使用XSLT 511 14.8.6 ...