国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

xml文件操作

2019-11-17 02:58:24
字體:
來源:轉載
供稿:網友

xml文件操作

    public static XmlDocument getDoc(String path)//加載xml文檔        {            XmlDocument doc = new XmlDocument();            doc.Load(path);            return doc;        }         /// <summary>        /// 返回找到的節點下標        /// </summary>        /// <param name="path">xml文件路徑</param>        /// <param name="bname">書名</param>        /// <returns></returns>        public static int getPosition(String path,string node, String bname)        {            int i;            XmlDocument doc = new XmlDocument();            doc.Load(path);            XmlNodeList nodeList = doc.SelectSingleNode(node).ChildNodes;            for (i = 0; i < nodeList.Count; i++)            {                if (nodeList[i].ChildNodes[0].InnerText == bname)                {                    return i;                }            }            return -1;        }

1.遍歷

        /// <summary>        /// 遍歷數據        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        PRotected void btnRead_Click(object sender, EventArgs e)        {            XmlDocument doc = getDoc("books.xml");//加載xml文檔,books文件存放于            foreach (XmlElement root in doc.DocumentElement.ChildNodes)//遍歷根的子節點            {                Response.Write("<scr

2.查找

        /// <summary>        /// 查找        /// </summary>        /// <param name="path">文件路徑</param>        /// <param name="node">節點</param>        /// <param name="bname">查找關鍵字</param>        /// <returns>XmlNode</returns>        public static XmlNode Find(string path,string node,string bname)        {            XmlDocument doc =  new XmlDocument();            doc.Load(path);//加載xml文檔            XmlNodeList nodeList = doc.SelectSingleNode(node).ChildNodes;            int i = getPosition(path, node, bname);//            if (i >= 0)                return nodeList[i];            else                return null;        }

3.刪除節點

        /// <summary>        /// 刪除元素、屬性        /// </summary>        /// <param name="path">文件</param>        /// <param name="node">指定節點的父節點</param>        /// <param name="attribute">為空時刪除節點,否則刪除屬性</param>        /// <param name="bname"></param>        public static void Delete(string path,string node,string attribute,string bname)        {            XmlDocument doc = new XmlDocument();            doc.Load(path);            XmlNode root = doc.SelectSingleNode(node);            XmlNodeList nodeList = doc.SelectSingleNode(node).ChildNodes;            int i = getPosition(path, node, bname);//返回指定的節點下標            if (i >= 0)            {                if (attribute.Equals(""))                {                    root.RemoveChild(nodeList[i]);                }                else                {                    XmlElement xn = (XmlElement)nodeList[i];                    xn.RemoveAttribute(attribute);                }            }            doc.Save(path);        }

4.添加

        /// <summary>        /// 添加元素值        /// </summary>        /// <param name="path"></param>        /// <param name="node"></param>        /// <param name="element"></param>        /// <param name="value"></param>        /// <param name="i">插入的下標,如果為負,默認從最后一個節點插入</param>        /// <returns></returns>        public static bool Add(string path,string node,string element,string value,int i)        {            XmlDocument doc = new XmlDocument();            doc.Load(path);            XmlNodeList nodeList = doc.SelectNodes(node);            XmlNode newNode = doc.SelectSingleNode(node).LastChild;            if (i < 0 || i > nodeList.Count-1)//如果小于0或者大于節點長度,默認從最后一個節點添加            {                XmlElement newElement = doc.CreateElement(element);//創建元素                newElement.InnerText = value;//賦值                newNode.AppendChild(newElement);            }            else            {                XmlElement newElement = doc.CreateElement(element);                newElement.InnerText = value;                nodeList[i - 1].AppendChild(newElement);            }            doc.Save(path);            return true;        }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 怀化市| 禹城市| 商都县| 四子王旗| 洱源县| 山丹县| 原阳县| 同德县| 长乐市| 绍兴县| 石渠县| 黄骅市| 巢湖市| 浠水县| 湘潭县| 钟祥市| 孝感市| 阿拉善右旗| 巴塘县| 沙坪坝区| 靖安县| 吉木萨尔县| 泾源县| 金川县| 太谷县| 图木舒克市| 义乌市| 龙海市| 都匀市| 海阳市| 广饶县| 东丰县| 牙克石市| 仙游县| 昔阳县| 凤庆县| 齐齐哈尔市| 腾冲县| 绍兴县| 怀化市| 怀化市|