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>
效果图: