C# 解析xml文件各个节点

1 篇文章 0 订阅
1 篇文章 0 订阅
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();

        }
    }
}
结果图:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值