中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

xml文檔操作類C#

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
    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  
                {  
                    //如果覆蓋模式,則強行創(chuàng)建一個xml文檔  
                    if (bOverWrite)  
                    {  
                        objXmlDoc.AppendChild(objXmlDoc.CreateXmlDeclaration("1.0", "utf-8", null));//設置xml的版本,格式信息  
                        objXmlDoc.AppendChild(objXmlDoc.CreateElement("", sRoot, ""));//創(chuàng)建根元素  
                        objXmlDoc.Save(XmlFile);//保存  
                    }  
                    else //否則,檢查文件是否存在,不存在則創(chuàng)建  
                    {  
                        if (!(File.Exists(XmlFile)))  
                        {  
                            objXmlDoc.AppendChild(objXmlDoc.CreateXmlDeclaration("1.0", "utf-8", null));  
                            objXmlDoc.AppendChild(objXmlDoc.CreateElement("", sRoot, ""));  
                            objXmlDoc.Save(XmlFile);  
                        }  
                    }  
                    objXmlDoc.Load(XmlFile);  
                }  
                catch (System.Exception ex)  
                {  
                    throw ex;  
                }  
                strXmlFile = XmlFile;  
            }  
      
      
      
            /// <summary>  
            /// 根據(jù)xPath值,返回xPath下的所有下級子結(jié)節(jié)到一個DataView  
            /// </summary>  
            /// <param name="XmlPathNode">xPath值</param>  
            /// <returns>有數(shù)據(jù)則返回DataView,否則返回null</returns>  
            public DataView GetData(string XmlPathNode)  
            {  
                //查找數(shù)據(jù)。返回一個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>  
            /// 更新節(jié)點內(nèi)容  
            /// </summary>  
            /// <param name="xmlPathNode"></param>  
            /// <param name="content"></param>  
            public void UpdateNode(string xmlPathNode, string content)  
            {  
                objXmlDoc.SelectSingleNode(xmlPathNode).InnerText = content;  
            }  
      
            /// <summary>  
            /// 更新節(jié)點的某個屬性  
            /// </summary>  
            /// <param name="xmlPathNode">要操作的節(jié)點</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>  
            /// 修改節(jié)點(同步更新內(nèi)容和屬性)  
            /// </summary>  
            /// <param name="xmlPathNode">要操作節(jié)點的xpath語句</param>  
            /// <param name="arrAttribName">屬性名稱字符串數(shù)組</param>  
            /// <param name="arrAttribContent">屬性內(nèi)容字符串數(shù)組</param>  
            /// <param name="content">節(jié)點內(nèi)容</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>  
            /// 移除選定節(jié)點集的所有屬性  
            /// </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)  
            {  
                //刪除一個節(jié)點。  
                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)  
            {  
                //插入一節(jié)點和此節(jié)點的一子節(jié)點。  
                XmlNode objRootNode = objXmlDoc.SelectSingleNode(mainNode);  
                XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);  
                objRootNode.AppendChild(objChildNode);//插入節(jié)點  
                XmlElement objElement = objXmlDoc.CreateElement(Element);  
                objElement.InnerText = Content;  
                objChildNode.AppendChild(objElement);//插入子節(jié)點  
            }  
      
            /// <summary>  
            /// 插入一個節(jié)點,帶一個Attribute和innerText  
            /// </summary>  
            /// <param name="mainNode"></param>  
            /// <param name="Element">節(jié)點名稱</param>  
            /// <param name="Attrib">Attribute名稱</param>  
            /// <param name="AttribContent">Attribute值</param>  
            /// <param name="Content">innerText值</param>  
            public void InsertNode(string mainNode, string Element, 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>  
            /// 插入一個節(jié)點,帶一個Attribute  
            /// </summary>  
            /// <param name="mainNode"></param>  
            /// <param name="Element">節(jié)點名稱</param>  
            /// <param name="Attrib">Attribute名稱</param>  
            /// <param name="AttribContent">Attribute值</param>     
            public void InsertNode(string mainNode, string Element, string Attrib, string AttribContent)  
            {  
                XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);  
                XmlElement objElement = objXmlDoc.CreateElement(Element);  
                objElement.SetAttribute(Attrib, AttribContent);  
                objNode.AppendChild(objElement);  
            }  
      
      
            /// <summary>  
            /// 插入一個節(jié)點  
            /// </summary>  
            /// <param name="mainNode"></param>  
            /// <param name="Element">節(jié)點名稱</param>         
            public void InsertNode(string mainNode, string Element)  
            {  
                XmlNode objNode = objXmlDoc.SelectSingleNode(mainNode);  
                XmlElement objElement = objXmlDoc.CreateElement(Element);  
                objNode.AppendChild(objElement);  
            }  
      
      
            //<summary>  
            //插入一個節(jié)點,帶多個屬性和一個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>  
            ///插入一個節(jié)點,帶多個屬性  
            ///</summary>  
            public void InsertNode(string mainNode, string elementName, string[] arrAttributeName, 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>  
            /// 插入子節(jié)點(帶多個屬性)  
            /// </summary>  
            /// <param name="parentNode">要插入的父節(jié)點</param>  
            /// <param name="elementName">插入的節(jié)點名稱</param>  
            /// <param name="arrAttributeName">屬性名稱[數(shù)組]</param>  
            /// <param name="arrAttributeContent">屬性內(nèi)容[數(shù)組]</param>  
            /// <param name="elementContent">節(jié)點內(nèi)容</param>  
            public void AddChildNode(string parentNodePath, string elementName, string[] arrAttributeName, string[] arrAttributeContent, 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>  
            /// 插入子節(jié)點(將內(nèi)容以CData形式寫入)  
            /// </summary>  
            /// <param name="parentNode">要插入的父節(jié)點</param>  
            /// <param name="elementName">插入的節(jié)點名稱</param>  
            /// <param name="elementContent">節(jié)點內(nèi)容</param>  
            public void AddChildNodeCData(string parentNodePath, string elementName, string elementContent)  
            {  
                try  
                {  
                    XmlNode parentNode = objXmlDoc.SelectSingleNode(parentNodePath);  
                    XmlElement objChildElement = objXmlDoc.CreateElement(elementName);  
      
                    //寫入cData數(shù)據(jù)  
                    XmlCDataSection xcds = objXmlDoc.CreateCDataSection(elementContent);  
      
                    objChildElement.AppendChild(xcds);  
                    parentNode.AppendChild(objChildElement);  
                }  
                catch  
                {  
                    return;  
                }  
      
            }  
      
      
            /// <summary>  
            /// 插入子節(jié)點(僅內(nèi)容,不帶屬性)  
            /// </summary>  
            /// <param name="parentNode">要插入的父節(jié)點</param>  
            /// <param name="elementName">插入的節(jié)點名稱</param>  
            /// <param name="elementContent">節(jié)點內(nèi)容</param>  
            public void AddChildNode(string parentNodePath, string elementName, string elementContent)  
            {  
                try  
                {  
                    XmlNode parentNode = objXmlDoc.SelectSingleNode(parentNodePath);  
                    XmlElement objChildElement = objXmlDoc.CreateElement(elementName);  
      
                    objChildElement.InnerText = elementContent;  
                    parentNode.AppendChild(objChildElement);  
                }  
                catch  
                {  
                    return;  
                }  
      
            }  
      
            /// <summary>  
            /// 根據(jù)xpath值查找節(jié)點  
            /// </summary>  
            /// <param name="NodePath">要查找節(jié)點的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;  
            }  
      
      
      
      
        }  
    }  
      
    調(diào)用方法  
     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", Shop.DAL.sp_netconfig.GetConfigObj().webname);  
                xc.AddChildNode("/rss/channel", "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() + "]", "title", ds.Tables[0].Rows[i]["pro_Name"].ToString());  
                    xc.AddChildNode("/rss/channel/item[" + j.ToString() + "]", "link", 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() + "]", "author", Shop.DAL.sp_netconfig.GetConfigObj().webname);  
                    xc.AddChildNodeCData("/rss/channel/item[" + j.ToString() + "]", "description", ds.Tables[0].Rows[i]["pro_Content"].ToString());  
                }  
                ds.Dispose();  
                xc.Save();  
                YZControl.staticFunction.FinalMessage("生成RSS成功!", "html.aspx", 0, 2);   

標簽: isp

版權(quán)申明:本站文章部分自網(wǎng)絡,如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:php檢測輸入數(shù)據(jù)是否合法常用的類

下一篇:c#異步Socket Tcp服務器實現(xiàn)