public XmlNode ToXmlNode(SiteSign sign) { XmlNode oldNode = null; XmlNode newNode = null; var xml = new XmlDocument(); xml.Load(@"SiteSign.xml"); { var root = xml.DocumentElement; foreach (XmlNode item in root.ChildNodes) { if (item.InnerText.Contains(sign.url)) { oldNode = item; break; } } newNode = xml.CreateElement("SiteSign"); Type t = sign.GetType(); PropertyInfo[] PropertyList = t.GetProperties(); foreach (PropertyInfo item in PropertyList) { string name = item.Name; object value = item.GetValue(sign, null); var xnode = xml.CreateElement(name); xnode.InnerText = SignToHtmlCode(value.ToString()); newNode.AppendChild(xnode); } //root.AppendChild(NewNode); if (oldNode == null) { root.InsertBefore(newNode, root.ChildNodes[0]); } else { root.ReplaceChild(newNode, oldNode); } xml.Save(@"SiteSign.xml"); GC.Collect(); } xml = null; return(newNode); }
/// <summary> /// 保存正文标志(json文件) /// </summary> /// <param name="sign"></param> public void SignToJson(SiteSign sign) { var data = GetSiteSigns(); var index = data.MESSAGE.SiteSign.FindIndex(p => p.url.Contains(sign.url)); if (index != -1) { data.MESSAGE.SiteSign.RemoveAt(index); //data.MESSAGE.SiteSign.Insert(index, sign); } else { index = 0; } data.MESSAGE.SiteSign.Insert(index, sign); //var setting = new Newtonsoft.Json.JsonSerializerSettings() { Formatting = Newtonsoft.Json.Formatting.Indented }; var json = JsonConvert.SerializeObject(data); CommonHelper.SaveToFile(@"SiteSign.json", json, Encoding.UTF8); }
/// <summary> /// 查询网站正文设定标志列表(xml文件) /// </summary> /// <returns></returns> public List <SiteSign> GetSiteSignList() { var list = new List <SiteSign>(); XHtmlAction xml = new XHtmlAction(); if (xml.Load(@"SiteSign.xml")) { var nodeList = xml.GetList("SiteSign"); foreach (XmlNode item in nodeList) { var sign = new SiteSign(); foreach (XmlNode node in item.ChildNodes) { string value = node.InnerXml .Replace("&", "&") .Replace("<", "<") .Replace(">", ">") .Replace(""", "\"") .Replace("'", "\'"); #region 赋值 switch (node.Name) { case "name": sign.name = value; break; case "url": sign.url = value; break; case "ListStart": sign.ListStart = value; break; case "ListEnd": sign.ListEnd = value; break; case "ContentStart": sign.ContentStart = value; break; case "ContentEnd": sign.ContentEnd = value; break; case "NeedDelStr": sign.NeedDelStr = value; break; case "VolumeStart": sign.VolumeStart = value; break; case "VolumeEnd": sign.VolumeEnd = value; break; case "BriefUrlStart": sign.BriefUrlStart = value; break; case "BriefUrlEnd": sign.BriefUrlEnd = value; break; case "AuthorStart": sign.AuthorStart = value; break; case "AuthorEnd": sign.AuthorEnd = value; break; case "BriefStart": sign.BriefStart = value; break; case "BriefEnd": sign.BriefEnd = value; break; case "BookImgUrlStart": sign.BookImgUrlStart = value; break; case "BookImgUrlEnd": sign.BookImgUrlEnd = value; break; default: break; } #endregion } list.Add(sign); } } ; return(list); }