C#.net 获取XML文件中某个时间标签判断

C#代码

 


导入命名空间

using System.Xml;

 

 

    /// <summary>
    /// 获取xml文件中的标签值做判断
    /// </summary>
    protected void GetXmlFile()
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("D://MyXML.xml");
        XmlNodeList nodelist = doc.SelectNodes("root/Action");
        int nodelistcount = nodelist.Count;
        int count = 0;//判断xml中不相等的数量
        string src = "";//获取与当前年份不相同的数据
        for (int i = 0; i < nodelistcount; i++)
        {
            if (DateTime.Now.ToString("yyyy-MM-dd") == nodelist.Item(i).SelectNodes("day").Item(0).InnerText)
            {
                src += nodelist.Item(i).SelectNodes("day").Item(0).InnerText + " 与当前年份相等!<br/>";
            }
            else
            {
                src += nodelist.Item(i).SelectNodes("day").Item(0).InnerText + ",";
                count++;
            }
        }
        src += count + " 个不与当前年份相等";
    }

 

 

 

XML文件


<?xml version="1.0" encoding="utf-8" ?>

<root>
<Action>
  <day name="day">2011-03-07</day>
</Action>

<Action>
  <day name="day">2011-03-08</day>
</Action>

<Action>
  <day name="day">2011-03-09</day>
</Action>

<Action>
  <day name="day">2011-03-10</day>
</Action>

<Action>
  <day name="day">2011-03-11</day>
</Action>
</root>

 

 

你可能感兴趣的:(xml,String,C#,encoding)