一。读取xml 如果时间不对就添加一个。如何对的就修改当前时间添加1.
public class RederXml { private static string path = AppDomain.CurrentDomain.BaseDirectory + "XML/TaoBaoNum.xml"; public static void Readxml() { using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { XmlDocument doc = new XmlDocument(); doc.Load(fs); XmlNode root = doc.SelectSingleNode("root"); XmlNode Num = root.SelectSingleNode(String.Format("Num[@name='{0}']", DateTime.Now.ToString("yyyy-MM-dd"))); if (Num == null) { XmlElement xe1 = doc.CreateElement("Num");//创建一个﹤book﹥节点 xe1.SetAttribute("name", DateTime.Now.ToString("yyyy-MM-dd"));//设置该节点genre属性 xe1.InnerText = "1";//设置节点的文本值 root.AppendChild(xe1); } else { long num = Convert.ToInt64(Num.InnerText.ToString()); num++; Num.InnerText = num.ToString(); } doc.Save(path);//保存。 } } }
xml
<root> <Num name="2015-04-08">0</Num> </root>
//调用
XML.RederXml.Readxml();