读取年份数组中的所有周六周天

第二次写随笔,并不是多么强大的功能,只是帮助自己记录一下一些完成的东西,以便后面使用。

前台按钮打印xml:

 <input type="button" value="生成日期xml" onclick="AddXML()"/>

 function AddXML() {
            $.ajax({
                type:"POST",
                url:"/XMLManage/CreatXML.aspx?Option=ADDXML",
                data: {},
                success: function() {
                    
                }
            });
        }

后台完成xml的创建和写入:

 public void AddXml()
        {
            XmlDocument xmldoc=new XmlDocument();
            XmlDeclaration xmldec = xmldoc.CreateXmlDeclaration("1.0", "GB2312", null);
            xmldoc.AppendChild(xmldec);
            XmlElement root = xmldoc.CreateElement("非工作日设置");
            xmldoc.AppendChild(root);
            XmlElement node = xmldoc.CreateElement("日期");
            node.SetAttribute("name", "notworkday");
            node.SetAttribute("id", "notworkday");
            List<string> DateList=new List<string>();
            DateList.Add("2015-01-01");
            DateList.Add("2016-01-01");
            DateList.Add("2017-01-01");
            //DateTime dt = DateTime.Parse("2015-01-01");
            for (int i = 0; i < DateList.Count; i++)
            {
                DateTime dt = DateTime.Parse(DateList[i]);
                while (dt.Year == DateTime.Parse(DateList[i].ToString()).Year)
                {
                    if (dt.DayOfWeek == DayOfWeek.Saturday)
                    {
                        var times = dt.Date.ToString("yyyy-MM-dd");
                        XmlElement element = xmldoc.CreateElement("非工作日");
                        element.SetAttribute("name", times);
                        element.SetAttribute("id", times);
                        node.AppendChild(element);
                    }
                    if (dt.DayOfWeek == DayOfWeek.Sunday)
                    {
                        var times = dt.Date.ToString("yyyy-MM-dd");
                        XmlElement element = xmldoc.CreateElement("非工作日");
                        element.SetAttribute("name", times);
                        element.SetAttribute("id", times);
                        node.AppendChild(element);
                    }
                    dt = dt.AddDays(1);
                    
                }
            }
            root.AppendChild(node);
            xmldoc.Save(@"E:\heheheh.xml");
        }

posted @ 2016-01-07 19:56  依恋guang  阅读(136)  评论(0编辑  收藏  举报