Linq 遍历获取XML节点中的信息

private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog oFD = new OpenFileDialog();
            oFD.Title = "打开文件";

            oFD.Filter = "xml (*.xml)|*.xml";//过滤格式
            oFD.FilterIndex = 1;                                    //格式索引
            oFD.RestoreDirectory = false;
            oFD.InitialDirectory = "c:\\";                          //默认路径
            oFD.Multiselect = true;                                 //是否多选
            if (oFD.ShowDialog() == DialogResult.OK)
            {

                MessageBox.Show(GetXmlNodeInformation(oFD.FileName).Count.ToString());
            }
        }
		private static  List<ChannelListInfo> GetXmlNodeInformation(string xmlPath)
		{
			List<ChannelListInfo> tvlist = new List<ChannelListInfo>();
      try
			{
				//定义并从xml文件中加载节点(根节点)
				XElement rootNode = XElement.Load(xmlPath);
				//查询语句: 获得根节点下name子节点(此时的子节点可以跨层次:孙节点、重孙节点)
				var channeltv = rootNode.Element("channel").Attribute("name").Value;
			  tvlist = (from p in rootNode.Descendants("program")
																				orderby p.Element("start").Value ascending
																				select new ChannelListInfo
																				{
																					WeekDate= CaculateWeekDay(DateTime.Parse(p.Element("start").Value).Year, DateTime.Parse(p.Element("start").Value).Month, DateTime.Parse(p.Element("start").Value).Day),
                                          ChannelTV = channeltv,
																					ChannelStartTime = p.Element("start").Value,
																					ChannelEndTime = p.Element("end").Value,
																					ChannelName = p.Element("name").Value,
																					ChannelType = p.Element("type").Value,
																					ChannelSummary = p.Element("summary").Value,
																				}).ToList<ChannelListInfo>();
				
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
			return tvlist;
		}

     /// 基姆拉尔森计算公式计算日期
    /// </summary>
    /// <param name="y">年</param>
    /// <param name="m">月</param>
    /// <param name="d">日</param>
    /// <returns>星期几</returns>
   
   protected static string CaculateWeekDay(int y, int m, int d)
		{
			if (m == 1 || m == 2)
			{
				m += 12;
				y--;         //把一月和二月看成是上一年的十三月和十四月,例:如果是2004-1-10则换算成:2003-13-10来代入公式计算。
			}
			int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;
			string weekstr = "";
			switch (week)
			{
				case 0: weekstr = "星期一"; break;
				case 1: weekstr = "星期二"; break;
				case 2: weekstr = "星期三"; break;
				case 3: weekstr = "星期四"; break;
				case 4: weekstr = "星期五"; break;
				case 5: weekstr = "星期六"; break;
				case 6: weekstr = "星期日"; break;
			}
			return weekstr;
		}
	}

	public class ChannelListInfo
	{
		public string WeekDate { get; set; }
		public string ChannelTV { get; set; }
		public string ChannelStartTime { get; set; }
		public string ChannelEndTime { get; set; }
		public string ChannelName { get; set; }
		public string ChannelType { get; set; }
		public string ChannelSummary { get; set; }
		
	}

Xml 文件信息:

<?xml version="1.0" encoding="UTF-8"?>
<epg>
  <channel name="中华美食">
    <program>
      <start>2016-03-25 00:26:00</start>
      <end>2016-03-25 06:00:00</end>
      <name>食在好源头</name>
      <type>饮食</type>
      <summary></summary>
    </program>
    <program>
      <start>2016-03-25 06:00:00</start>
      <end>2016-03-25 06:10:00</end>
      <name>走读江南</name>
      <type></type>
      <summary></summary>
    </program>
    <program>
      <start>2016-03-25 06:10:00</start>
      <end>2016-03-25 06:45:00</end>
      <name>时尚美食</name>
      <type></type>
      <summary></summary>
    </program>
    <program>
      <start>2016-03-25 06:45:00</start>
      <end>2016-03-25 07:05:00</end>
      <name>我的导游是明星</name>
      <type></type>
      <summary></summary>
    </program>
    <program>
      <start>2016-03-25 07:05:00</start>
      <end>2016-03-25 07:45:00</end>
      <name>时尚旅游</name>
      <type></type>
      <summary></summary>
    </program>
    <program>
      <start>2016-03-25 07:45:00</start>
      <end>2016-03-25 08:30:00</end>
      <name>名厨甄文达</name>
      <type></type>
      <summary></summary>
    </program>
    <program>
      <start>2016-03-25 08:30:00</start>
      <end>2016-03-25 09:00:00</end>
      <name>美味人生</name>
      <type>家庭</type>
      <summary></summary>
    </program>
  
  </channel>
</epg>

效果图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值