下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
using System; using System.Xml; using System.Configuration; using System.Data; using System.Collections; using System.IO; namespace YZControl { public class NewXmlControl : Object { protected string strXmlfile; protected Xmldocument objXmlDoc = new Xmldocument(); public NewXmlControl(string Xmlfile,Boolean bOverWrite,string sRoot) { try { //如果覆盖模式,则强行创建一个xml文档 if (bOverWrite) { objXmlDoc.AppendChild(objXmlDoc.CreateXmlDeclaration("1.0","utf-8",null));//设置xml的版本,格式信息 objXmlDoc.AppendChild(objXmlDoc.CreateElement("",sRoot,""));//创建根元素 objXmlDoc.Save(Xmlfile);//保存 } else //否则,检查文件是否存在,不存在则创建 { if (!(file.Exists(Xmlfile))) { objXmlDoc.AppendChild(objXmlDoc.CreateXmlDeclaration("1.0",null)); objXmlDoc.AppendChild(objXmlDoc.CreateElement("","")); objXmlDoc.Save(Xmlfile); } } objXmlDoc.Load(Xmlfile); } catch (System.Exception ex) { throw ex; } strXmlfile = Xmlfile; } /// <summary> /// 根据xPath值,返回xPath下的所有下级子结节到一个DataVIEw /// </summary> /// <param name="XmlPathNode">xPath值</param> /// <returns>有数据则返回DataVIEw,否则返回null</returns> public DataVIEw GetData(string XmlPathNode) { //查找数据。返回一个DataVIEw DataSet ds = new DataSet(); try { StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml); ds.readxml(read); return ds.tables[0].defaultview; } catch { //throw; return null; } } /// <summary> /// 更新节点内容 /// </summary> /// <param name="xmlPathNode"></param> /// <param name="content"></param> public voID UpdateNode(string xmlPathNode,string content) { objXmlDoc.SelectSingleNode(xmlPathNode).InnerText = content; } /// <summary> /// 更新节点的某个属性 /// </summary> /// <param name="xmlPathNode">要 *** 作的节点</param> /// <param name="Attribname">属性名</param> /// <param name="AttribValue">属性值</param> public voID UpdateNode(string xmlPathNode,string Attribname,string AttribValue) { ((XmlElement)(objXmlDoc.SelectSingleNode(xmlPathNode))).SetAttribute(Attribname,AttribValue); } /// <summary> /// 修改节点(同步更新内容和属性) /// </summary> /// <param name="xmlPathNode">要 *** 作节点的xpath语句</param> /// <param name="arrAttribname">属性名称字符串数组</param> /// <param name="arrAttribContent">属性内容字符串数组</param> /// <param name="content">节点内容</param> public voID UpdateNode(string xmlPathNode,string[] arrAttribname,string[] arrAttribContent,string content) { XmlNode xn = objXmlDoc.SelectSingleNode(xmlPathNode); if (xn != null) { xn.InnerText = content; xn.Attributes.RemoveAll(); for (int i = 0; i <= arrAttribname.GetUpperBound(0); i++) { ((XmlElement)(xn)).SetAttribute(arrAttribname[i],arrAttribContent[i]); } } } /// <summary> /// 移除选定节点集的所有属性 /// </summary> /// <param name="xmlPathNode"></param> public voID RemoveAllAttribute(string xmlPathNode) { XmlNodeList xnl = objXmlDoc.SelectNodes(xmlPathNode); foreach (XmlNode xn in xnl) { xn.Attributes.RemoveAll(); } } public voID DeleteNode(string Node) { //刪除一个节点。 try { string mainNode = Node.Substring(0,Node.LastIndexOf("/")); objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node)); } catch { //throw; return; } } public voID InsertNodeWithChild(string mainNode,string ChildNode,string Element,string Content) { //插入一节点和此节点的一子节点。 XmlNode objRootNode = objXmlDoc.SelectSingleNode(mainNode); XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode); objRootNode.AppendChild(objChildNode);//插入节点 XmlElement objElement = objXmlDoc.CreateElement(Element); objElement.InnerText = Content; objChildNode.AppendChild(objElement);//插入子节点 } /// <summary> /// 插入一个节点,带一个Attribute和innerText /// </summary> /// <param name="mainNode"></param> /// <param name="Element">节点名称</param> /// <param name="Attrib">Attribute名称</param> /// <param name="AttribContent">Attribute值</param> /// <param name="Content">innerText值</param> public voID InsertNode(string mainNode,string Attrib,string AttribContent,string Content) { XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode); XmlElement objElement = objXmlDoc.CreateElement(Element); objElement.SetAttribute(Attrib,AttribContent); objElement.InnerText = Content; objNode.AppendChild(objElement); } /// <summary> /// 插入一个节点,带一个Attribute /// </summary> /// <param name="mainNode"></param> /// <param name="Element">节点名称</param> /// <param name="Attrib">Attribute名称</param> /// <param name="AttribContent">Attribute值</param> public voID InsertNode(string mainNode,string AttribContent) { XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode); XmlElement objElement = objXmlDoc.CreateElement(Element); objElement.SetAttribute(Attrib,AttribContent); objNode.AppendChild(objElement); } /// <summary> /// 插入一个节点 /// </summary> /// <param name="mainNode"></param> /// <param name="Element">节点名称</param> public voID InsertNode(string mainNode,string Element) { XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode); XmlElement objElement = objXmlDoc.CreateElement(Element); objNode.AppendChild(objElement); } //<summary> //插入一个节点,带多个属性和一个inner text //</summary> public voID InsertNode(string mainNode,string elementname,string[] arrAttributename,string[] arrAttributeContent,string elementContent) { try { XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode); XmlElement objElement = objXmlDoc.CreateElement(elementname); for (int i = 0; i <= arrAttributename.GetUpperBound(0); i++) { objElement.SetAttribute(arrAttributename[i],arrAttributeContent[i]); } objElement.InnerText = elementContent; objNode.AppendChild(objElement); } catch { throw; //string t = mainNode; //; } } ///<summary> ///插入一个节点,带多个属性 ///</summary> public voID InsertNode(string mainNode,string[] arrAttributeContent) { try { XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode); XmlElement objElement = objXmlDoc.CreateElement(elementname); for (int i = 0; i <= arrAttributename.GetUpperBound(0); i++) { objElement.SetAttribute(arrAttributename[i],arrAttributeContent[i]); } //objElement.InnerText = elementContent; objNode.AppendChild(objElement); } catch { throw; //string t = mainNode; //; } } /// <summary> /// 插入子节点(带多个属性) /// </summary> /// <param name="parentNode">要插入的父节点</param> /// <param name="elementname">插入的节点名称</param> /// <param name="arrAttributename">属性名称[数组]</param> /// <param name="arrAttributeContent">属性内容[数组]</param> /// <param name="elementContent">节点内容</param> public voID AddChildNode(string parentNodePath,string elementContent) { try { XmlNode parentNode = objXmlDoc.SelectSingleNode(parentNodePath); XmlElement objChildElement = objXmlDoc.CreateElement(elementname); for (int i = 0; i <= arrAttributename.GetUpperBound(0); i++) { objChildElement.SetAttribute(arrAttributename[i],arrAttributeContent[i]); } objChildElement.InnerText = elementContent; parentNode.AppendChild(objChildElement); } catch { return; } } /// <summary> /// 插入子节点(将内容以cdata形式写入) /// </summary> /// <param name="parentNode">要插入的父节点</param> /// <param name="elementname">插入的节点名称</param> /// <param name="elementContent">节点内容</param> public voID AddChildNodecdata(string parentNodePath,string elementContent) { try { XmlNode parentNode = objXmlDoc.SelectSingleNode(parentNodePath); XmlElement objChildElement = objXmlDoc.CreateElement(elementname); //写入cdata数据 XmlcdataSection xcds = objXmlDoc.CreatecdataSection(elementContent); objChildElement.AppendChild(xcds); parentNode.AppendChild(objChildElement); } catch { return; } } /// <summary> /// 插入子节点(仅内容,不带属性) /// </summary> /// <param name="parentNode">要插入的父节点</param> /// <param name="elementname">插入的节点名称</param> /// <param name="elementContent">节点内容</param> public voID AddChildNode(string parentNodePath,string elementContent) { try { XmlNode parentNode = objXmlDoc.SelectSingleNode(parentNodePath); XmlElement objChildElement = objXmlDoc.CreateElement(elementname); objChildElement.InnerText = elementContent; parentNode.AppendChild(objChildElement); } catch { return; } } /// <summary> /// 根据xpath值查找节点 /// </summary> /// <param name="NodePath">要查找节点的xpath值</param> /// <returns>找到返回true,否则返回true</returns> public bool FindNode(string NodePath) { try { if (objXmlDoc.SelectSingleNode(NodePath) != null) { return true; } else { return false; } } catch { return false; } } /// <summary> ///保存文档 /// </summary> public voID Save() { //保存文档。 try { objXmlDoc.Save(strXmlfile); } catch (System.Exception ex) { throw ex; } objXmlDoc = null; } } } 调用方法 NewXmlControl xc = new NewXmlControl(Server.MapPath("~/RSS.xml"),true,"RSS"); xc.UpdateNode("//RSS","version","2.0"); xc.InsertNode("//RSS","channel"); xc.AddChildNode("/RSS/channel","Title",Shop.DAL.sp_netconfig.GetConfigObj().webname); // xc.AddChildNode("/RSS/channel","slogan",Shop.DAL.sp_netconfig.GetConfigObj().webname); xc.AddChildNode("/RSS/channel","link",Shop.DAL.sp_netconfig.GetConfigObj().weburl); xc.AddChildNode("/RSS/channel","language","zh-cn"); xc.AddChildNode("/RSS/channel","description",Shop.DAL.sp_netconfig.GetConfigObj().Metatwo); // xc.AddChildNode("/RSS/channel","copyright",Shop.DAL.sp_netconfig.GetConfigObj().copyright); xc.AddChildNode("/RSS/channel","author","generator","RSS Generator By Taoxian"); DataSet ds = DbHelpersql.query("select top 20 pro_ID,pro_name,pro_CreateTime,pro_Content from sp_product where pro_SaleType=1 and pro_Stock>0 and pro_Audit=1 order by pro_ID desc"); for (int i = 0; i < ds.tables[0].Rows.Count; i++) { int j = i + 1; xc.InsertNode("/RSS/channel","item"); xc.AddChildNode("/RSS/channel/item[" + j.ToString() + "]",ds.tables[0].Rows[i]["pro_name"].ToString()); xc.AddChildNode("/RSS/channel/item[" + j.ToString() + "]",Shop.DAL.sp_netconfig.GetConfigObj().weburl + "/Product/ProductInfo_" + ds.tables[0].Rows[i]["pro_ID"].ToString() + ".HTML"); xc.AddChildNode("/RSS/channel/item[" + j.ToString() + "]","pubDate",Convert.ToDateTime(ds.tables[0].Rows[i]["pro_CreateTime"].ToString()).GetDateTimeFormats('r')[0].ToString()); xc.AddChildNode("/RSS/channel/item[" + j.ToString() + "]",Shop.DAL.sp_netconfig.GetConfigObj().webname); xc.AddChildNodecdata("/RSS/channel/item[" + j.ToString() + "]",ds.tables[0].Rows[i]["pro_Content"].ToString()); } ds.dispose(); xc.Save(); YZControl.staticFunction.FinalMessage("生成RSS成功!","HTML.aspx",2);
以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的xml文档 *** 作类C#全部内容,希望文章能够帮你解决xml文档 *** 作类C#所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)