C#實現XML的增刪查改

命名空間:using System.Xml;

1、查找某個節點是否存在:

    private XmlDocument xmldoc;
    private string mod="1";  
    private bool findNode()
 {
       xmldoc = new XmlDocument();

       xmldoc.Load("D://a.xml");      //加載xml文檔內容,路徑為xml路徑

       XmlNode s = xmldoc.SelectSingleNode("//" + "model");//第一層節點
                if (s.HasChildNodes)
                {
                    XmlNodeList xnl = s.ChildNodes;//子節點列表
                    foreach (XmlNode xn in xnl)
                    {
                        XmlElement xn1 = (XmlElement)xn;//節點轉換為元素
                        if (xn1.Name == mod)//節點找到,返回true
                        {
                            return true;
                        }
                    }
                    return false;//節點沒找到,返回false
                }
}

 2、增加節點:

      添加節點,如果有多級節點,從n級節點往上添加節點的順序添加。

   private static XmlElement xmlem;
public bool AddNode( ) { xmldoc = new XmlDocument(); if (!File.Exists("D://a.xml"))//xml文件不存在 { XmlDeclaration xmldecl; xmldecl = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null); //xml頂部聲明 xmldoc.AppendChild(xmldecl);//xml添加頭部聲明 xmlem = xmldoc.CreateElement("", "model", ""); //創建第一級元素 XmlElement xe8= xmldoc.CreateElement(mod); //創建第一級節點的子節點,即第二級節點,要添加的節點 xmlem.AppendChild(xe8); //在第一級節點追加第二級節點 xmldoc.AppendChild(xmlem); //文件中添加添加第一級節點和它的子節點 } else//xml文件存在 { xmldoc.Load(Sett.pahXml)//加載xml內容 if (!findNode()) //查找節點不存在 { XmlNode rot = xmldoc.SelectSingleNode("//model"); //挑選第一個節點 xmlem = xmldoc.CreateElement(mod); //創建要添加的節點 rot.AppendChild(xmlem); //添加節點 xmldoc.AppendChild(rot); } } xmldoc.Save("D://a.xml"); return true; }

 3、刪除節點:  

     public  bool RemoveNode()
     {
                xmldoc = new XmlDocument();    
                if (!File.Exists("D://a.xml"))
                {
                    return true;
                }
                else
                {
                    xmldoc.Load(Sett.pahXml);
                    if (findNode())//節點存在
                    {           
                            XmlNode root = xmldoc.SelectSingleNode("//model/"+Sett.station.mod);//查找父節點
                            XmlNodeList nl = root.ChildNodes;
                            foreach (XmlNode no in nl)
                            {
                                XmlElement f = (XmlElement)no;
                                if (f.Name ==  station.ID)
                                {
                                    root.RemoveChild(f);//刪除節點
                                    break;
                                }
                            }
                             xmldoc.Save(Sett.pahXml);
                             return true;
                        }
                        else
                        {
                            return true;
                        }
                }
        }    

 4、修改節點:

 private bool modifyNode()
{
     xmldoc.Load(Sett.pahXml);             
                    if (findmolde())
                    {
                        XmlNode root = xmldoc.SelectSingleNode("//model/" + station.mod);//上級節點
                        if(findID())//查找節點是否存在
                        {
                            XmlNodeList nl = root.ChildNodes;
                            foreach(XmlNode no in nl)
                            {
                                XmlElement f = (XmlElement)no;
                                if(f.Name==station.ID)//查找到要修改的節點
                                {
                                    f.SetAttribute("name", station.Name);//修改屬性
                                    f.SetAttribute("Time", station.time.ToString());
                                    f.SetAttribute("UPH", station.UPH.ToString());
                                    XmlNodeList nls = f.ChildNodes;
                                    foreach(XmlNode no2 in nls)//修改第一級子節點
                                    {
                                        XmlElement g = (XmlElement)no2;
                                        if (g.Name == "Function")
                                        {
                                            g.InnerText = station.Func;
                                            continue;
                                        }
                                        if (g.Name == "Leader")
                                        {
                                            g.InnerText = station.Leader;
                                            continue;
                                        }
                                        if (g.Name =="ProcStep")
                                        {
                                            g.InnerText = station.Proc;
                                            continue;
                                        }
                                        if(g.Name=="Set")
                                        {
                                            XmlNodeList h = g.ChildNodes;//修改第二級子節點
                                            g.RemoveAll();
                                            foreach(KeyValuePair<string,int> kv in station.set)
                                            {
                                                    string dd = kv.Key;
                                                    XmlElement xe5 = xmldoc.CreateElement(dd);
                                                    xe5.InnerText = kv.Value.ToString();
                                                    g.AppendChild(xe5);
                                            }                                                             
                                       }
                                    }
                                   xmldoc.Save(Sett.pahXml);return true;
                                }else{return false;}
                            }else{return false;}
}

 

https://www.cnblogs.com/guxia/p/8242483.html

原文地址:https://www.cnblogs.com/Zhengxiaoxiao/p/10298043.html