xml文件的建立、修改、刪除與查找

 早幾天做了一個用xml文件存儲友情鏈接的程序,xml文件內容如下:

 <?xml version="1.0" encoding="gb2312"?>
<root>
  <link id="1">
    <linkid>1</linkid>
    <sitename>華中科技大學東莞研究院 </sitename>
    <linkurl>http://www.dghust.com</linkurl>
  </link>
   <link id="2">
    <linkid>2</linkid>
    <sitename>e-works </sitename>
    <linkurl>http://www.e-works.net.cn</linkurl>
  </link>
   <link id="3">
    <linkid>3</linkid>
    <sitename>新浪 </sitename>
    <linkurl>http://www.sina.com.cn</linkurl>
  </link>
</root>

下面代碼實現了增加、修改、刪除友情鏈接

private void CreateLinkXml(string sitename, string linkurl)
        { 

          string xmlFilePath = "xml/friendlink.xml";

          string xmlFile = Server.MapPath(xmlFilePath);
            XmlDocument xmlDoc = new XmlDocument();
       
          if (!File.Exists(xmlFile))//創建xml文件
            {//創建xml文件
                XmlTextWriter objX = new XmlTextWriter(xmlFile, System.Text.Encoding.GetEncoding("GB2312"));
                objX.Formatting = Formatting.Indented;
                objX.WriteStartDocument();
                objX.WriteStartElement("root");//創建根目錄
                objX.WriteEndElement();
                objX.Close();
            }
            xmlDoc.Load(xmlFile);              
            XmlNode e_rss = xmlDoc.SelectSingleNode("root");
            int i ;
            XmlNode lastNode = e_rss.LastChild;
            if (lastNode != null)
            {
                i = int.Parse(lastNode.FirstChild.InnerText) + 1;//將最後一個節點linkid的值加1賦給新加的節點linkid的值
            }
            else
                i = 1;
           
            //創建新的節點元素link
            XmlElement e_node = xmlDoc.CreateElement("link");
            e_node.SetAttribute("id", i.ToString());


            XmlElement e_linkid = xmlDoc.CreateElement("linkid");
            e_linkid.InnerText = i.ToString();      
            e_node.AppendChild(e_linkid);//在link節點下新加linkid子節點

            XmlElement e_title = xmlDoc.CreateElement("sitename");
            e_title.InnerText = sitename;
            e_node.AppendChild(e_title);//在link節點下新加sitename子節點

            XmlElement e_link = xmlDoc.CreateElement("linkurl");
            e_link.InnerText = linkurl;
            e_node.AppendChild(e_link);//在link節點下新加linkurl子節點
         
            e_rss.AppendChild(e_node);

            xmlDoc.Save(xmlFile);
            xmlDoc = null;
        }
        //編輯
        private void ModifyXml(string linkId, string sitename, string linkurl)
        {
            XmlDocument xmlDoc = new XmlDocument();
            string xmlFile = Server.MapPath(xmlFilePath);
            xmlDoc.Load(xmlFile);
            //XmlNode nodecp = xmlDoc.SelectSingleNode("/root/link/linkid[text()='"+linkId+"']");
            //找到link節點屬性值等於linkId的節點
            XmlNode nodecp = xmlDoc.SelectSingleNode("/root/link[@id='" + linkId + "']");
            if (nodecp != null)
            {
                XmlNodeList nodlist = nodecp.ChildNodes;
                if (nodlist != null)
                {
                    foreach (XmlNode MyNode in nodlist)
                    {

                        if (MyNode.Name == "sitename")
                        {
                            MyNode.InnerText = sitename;
                        }
                        if (MyNode.Name == "linkurl")
                        {
                            MyNode.InnerText = linkurl;
                        }

                                    }
                }
            }
               
            xmlDoc.Save(xmlFile);
            xmlDoc = null;
        }
        //刪除
        private void DeleteXml(string linkId)
        {
            XmlDocument xmlDoc = new XmlDocument();
            string xmlFile = Server.MapPath(xmlFilePath);
            xmlDoc.Load(xmlFile);

            XmlNode nodecp = xmlDoc.SelectSingleNode("/root/link[@id='" + linkId + "']");
            XmlNode parent = nodecp.ParentNode;
            parent.RemoveChild(nodecp);
            //nodecp.RemoveAll();
            xmlDoc.Save(xmlFile);
            xmlDoc = null;
        }

  /// <summary>
        /// 根據xml節點屬性找到節點另一個屬性值
        /// </summary>
        /// <param name="valuestring">節點的已知屬性值</param>
        /// <param name="xmlFile">xml文件(包括路徑)</param>
        /// <param name="rootnode">節點路徑</param>
        /// <param name="attr">已知屬性名</param>
        /// <param name="valuename">想得到值的屬性名</param>
        /// <returns></returns>
         public static  string GetXmlNodeValue(string valuestring, string xmlFile, string rootnode,string attr,string valuename)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlFile =System.Web.HttpContext.Current.Server.MapPath(xmlFile);
            xmlDoc.Load(xmlFile);
            
            XmlNode nodecp = xmlDoc.SelectSingleNode(""+rootnode+"[@"+attr+"='" + valuestring + "']");
            return nodecp.Attributes[""+valuename+""].Value;
            }

     /// <summary>
        /// 搜索xml含有特定字符的屬性節點的另一屬性值      

       /// </summary>
        /// <param name="valuestring">輸入的字符</param>
        /// <param name="xmlFile">xml文件(包括路徑)</param>
        /// <param name="rootnode">xml根節點</param>
        /// <param name="attr">搜索是否含有字符的屬性名</param>
        /// <param name="valuename">想得到值的屬性名</param>
        /// <returns></returns>

 public static string SerarchXmlValue(string valuestring, string xmlFile, string rootnode,string attr,string valuename)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(System.Web.HttpContext.Current.Server.MapPath(xmlFile));
            XmlNodeList topM = xmlDoc.DocumentElement.ChildNodes;

            foreach (XmlElement element in topM)
            {
                if (element.Name.ToLower() == rootnode)
                {
                    //得到該節點的子節點集
                    XmlNodeList nodelist = element.ChildNodes;

                    if (nodelist.Count > 0)
                    {
                        foreach (XmlElement el in nodelist)//讀元素值
                        {
                            if (el.Attributes[""+attr+""].Value.IndexOf(valuestring)!=-1)
                            {
                                return el.Attributes[""+valuename+""].Value;
                            }

                        }

                    }
                }
            }
            return "";

        }
      

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章