using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;
namespace xml解析
{
class Program
{
static void Main(string[] args)
{
string xml = "WeatherInfo.xml";
string finalstr= File.ReadAllText(xml, Encoding.UTF8);
XDocument xdoc = XDocument.Load(new StringReader(finalstr));
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.WriteLine(status);
Console.WriteLine(currentdate);
Console.WriteLine(currentCity);
Console.WriteLine(date1);
Console.WriteLine(dayPictureUrl1);
Console.WriteLine(nightPictureUrl);
Console.WriteLine(weather1);
Console.WriteLine(wind1);
Console.WriteLine(temperature1);
Console.WriteLine(date2);
Console.WriteLine(dayPictureUr2);
Console.WriteLine(nightPictureUrl2);
Console.WriteLine(weather2);
Console.WriteLine(wind2);
Console.WriteLine(temperature2);
Console.ReadKey();
}
}
}
结果图:
C# 解析xml文件各个节点
最新推荐文章于 2023-01-13 10:27:00 发布