C# 解析xml 取出各个节点的值。
<?xmlversion="1.0"encoding="utf-8"?>-<CityWeatherResponse><status>success</status><da...
<?xml
version="1.0" encoding="utf-8" ?>
- <CityWeatherResponse>
<status>success</status>
<date>2014-05-03</date>
- <results>
<currentCity>濮阳</currentCity>
- <weather_data>
<date>周六(今天, 实时:17℃)</date>
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/duoyun.png</dayPictureUrl>
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/duoyun.png</nightPictureUrl>
<weather>多云</weather>
<wind>微风</wind>
<temperature>9℃</temperature>
<date>周日</date>
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/qing.png</dayPictureUrl>
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/qing.png</nightPictureUrl>
<weather>晴</weather>
<wind>微风</wind>
<temperature>22 ~ 7℃</temperature>
<date>周一</date>
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/qing.png</dayPictureUrl>
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/qing.png</nightPictureUrl>
<weather>晴</weather>
<wind>微风</wind>
<temperature>24 ~ 11℃</temperature>
<date>周二</date>
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/duoyun.png</dayPictureUrl>
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/qing.png</nightPictureUrl>
<weather>多云转晴</weather>
<wind>微风</wind>
<temperature>26 ~ 15℃</temperature>
</weather_data>
</results>
</CityWeatherResponse> 展开
version="1.0" encoding="utf-8" ?>
- <CityWeatherResponse>
<status>success</status>
<date>2014-05-03</date>
- <results>
<currentCity>濮阳</currentCity>
- <weather_data>
<date>周六(今天, 实时:17℃)</date>
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/duoyun.png</dayPictureUrl>
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/duoyun.png</nightPictureUrl>
<weather>多云</weather>
<wind>微风</wind>
<temperature>9℃</temperature>
<date>周日</date>
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/qing.png</dayPictureUrl>
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/qing.png</nightPictureUrl>
<weather>晴</weather>
<wind>微风</wind>
<temperature>22 ~ 7℃</temperature>
<date>周一</date>
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/qing.png</dayPictureUrl>
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/qing.png</nightPictureUrl>
<weather>晴</weather>
<wind>微风</wind>
<temperature>24 ~ 11℃</temperature>
<date>周二</date>
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/duoyun.png</dayPictureUrl>
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/qing.png</nightPictureUrl>
<weather>多云转晴</weather>
<wind>微风</wind>
<temperature>26 ~ 15℃</temperature>
</weather_data>
</results>
</CityWeatherResponse> 展开
展开全部
using System.Xml.Linq;
using System.IO;
string xml = @"<?xml
version=""1.0"" encoding=""utf-8"" ?>
<CityWeatherResponse>
<status>success</status>
<date>2014-05-03</date>
<results>
<currentCity>濮阳</currentCity>
<weather_data>
<date>周六(今天, 实时:17℃)</date>
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/duoyun.png</dayPictureUrl>
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/duoyun.png</nightPictureUrl>
<weather>多云</weather>
<wind>微风</wind>
<temperature>9℃</temperature>
<date>周日</date>
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/qing.png</dayPictureUrl>
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/qing.png</nightPictureUrl>
<weather>晴</weather>
<wind>微风</wind>
<temperature>22 ~ 7℃</temperature>
<date>周一</date>
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/qing.png</dayPictureUrl>
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/qing.png</nightPictureUrl>
<weather>晴</weather>
<wind>微风</wind>
<temperature>24 ~ 11℃</temperature>
<date>周二</date>
<dayPictureUrl>http://api.map.baidu.com/images/weather/day/duoyun.png</dayPictureUrl>
<nightPictureUrl>http://api.map.baidu.com/images/weather/night/qing.png</nightPictureUrl>
<weather>多云转晴</weather>
<wind>微风</wind>
<temperature>26 ~ 15℃</temperature>
</weather_data>
</results>
</CityWeatherResponse>";
XDocument xdoc = XDocument.Load(new StringReader(xml));
XElement root = xdoc.Elements().First();
XElement result = root.Element("results");
XElement weather_data = result.Element("weather_data");
//以下是各节点输出
string status = root.Element("status").Value;
string currentdate = root.Element("date").Value;
string currentCity = result.Element("currentCity").Value;
string date1 = weather_data.Element("date").Value;
string dayPictureUrl1 = weather_data.Element("dayPictureUrl").Value;
string nightPictureUrl = weather_data.Element("nightPictureUrl").Value;
string weather1 = weather_data.Element("weather").Value;
string wind1 = weather_data.Element("wind").Value;
string temperature1 = weather_data.Element("temperature").Value;
string date2 = weather_data.Elements("date").Skip(1).First().Value;
string dayPictureUr2 = weather_data.Elements("dayPictureUrl").Skip(1).First().Value;
string nightPictureUrl2 = weather_data.Elements("nightPictureUrl").Skip(1).First().Value;
string weather2 = weather_data.Elements("weather").Skip(1).First().Value;
string wind2 = weather_data.Elements("wind").Skip(1).First().Value;
string temperature2 = weather_data.Elements("temperature").Skip(1).First().Value;
string date3 = weather_data.Elements("date").Skip(2).First().Value;
string dayPictureUrl3 = weather_data.Elements("dayPictureUrl").Skip(2).First().Value;
string nightPictureUrl3 = weather_data.Elements("nightPictureUrl").Skip(2).First().Value;
string weather3 = weather_data.Elements("weather").Skip(2).First().Value;
string wind3 = weather_data.Elements("wind").Skip(2).First().Value;
string temperature3 = weather_data.Elements("temperature").Skip(2).First().Value;
string date4 = weather_data.Elements("date").Skip(3).First().Value;
string dayPictureUrl4 = weather_data.Elements("dayPictureUrl").Skip(3).First().Value;
string nightPictureUrl4 = weather_data.Elements("nightPictureUrl").Skip(3).First().Value;
string weather4 = weather_data.Elements("weather").Skip(3).First().Value;
string wind4 = weather_data.Elements("wind").Skip(3).First().Value;
string temperature4 = weather_data.Elements("temperature").Skip(3).First().Value;
Console.ReadKey();
展开全部
(?<=<(?<key>\w+)>)[^<>]*(?=</\k<key>>) 正则表达式 可以匹配最小的单元
如<>111</>
得到111
如<>111</>
得到111
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你需要网络搜索一个xml读取的例子代码学习
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询